andbook!.pdf - Learning Android Get an anddev.org - Android-Shirt Back to index
anddev.org Header Logo
FAQ Search Top rated articles Browse Feeds anddev.org - Authors Contact Details Register Log in

Upload Files to Web Server

Goto page 1, 2, 3, 4  Next
 
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials
Author Message
venkat
Senior Developer


Joined: 27 Nov 2007
Posts: 152
Location: India

PostPosted: Sat Jan 05, 2008 7:54 pm    Post subject: Upload Files to Web Server Reply with quote

Hi to All,
is it possible to upload files from android to web server, In web server i am using Php file to upload files.

This is sample php file to upload files to web server.

php:
<?php

$target_path =  basename( $_FILES["uploadedfile"]["name"]);
if(move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], $target_path)) {
echo "The file ".basename( $_FILES["uploadedfile"]["name"])." has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}


?>


Using above Php code Rolling Eyes, i can upload files from j2me. Can you any one tell me how to upload files from android. Question

Thanks and Regards, Smile
Venkat.
Back to top
View user's profile Send private message
ramonrabello
Freshman


Joined: 05 Jan 2008
Posts: 4

PostPosted: Sat Jan 05, 2008 8:59 pm    Post subject: Re: Upload Files to Web Server Reply with quote

venkat wrote:
Hi to All,
is it possible to upload files from android to web server, In web server i am using Php file to upload files.

This is sample php file to upload files to web server.

Code:
<?php

$target_path =  basename( $_FILES["uploadedfile"]["name"]);
if(move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], $target_path)) {
echo "The file ".basename( $_FILES["uploadedfile"]["name"])." has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}


?>


Using above Php code Rolling Eyes, i can upload files from j2me. Can you any one tell me how to upload files from android. Question

Thanks and Regards, Smile
Venkat.


Well, if u need to upload I think that u need to upload the .apk (deploy file of Android) instead a .jar or a .jad file like in j2me. But u have to update your web server configuration to accept the MIME type .apk to allow the upload.
Back to top
View user's profile Send private message
venkat
Senior Developer


Joined: 27 Nov 2007
Posts: 152
Location: India

PostPosted: Sat Jan 05, 2008 9:11 pm    Post subject: Reply with quote

Hi ramonrabello,
Thanks for your reply Smile, Actually, php code will run on Web server. we just need to pass parameter to that php code. Using below code i can pass data to the php code. Smile

Java:
                    try
                    {
                         HttpClient client = new HttpClient();
                         HttpMethod method = new PostMethod("http://webserver.com/data.php?data=myData");

                         client.executeMethod(method);
                         a.setText("Ok"+method.getResponseBodyAsString());
                         method.releaseConnection();
                    }
                    catch (Exception e)
                    {
                         a.setText("Error");
                    }


but, I want to upload File. Can any one tell me, how to do it?? Rolling Eyes

Thanks and regads, Smile
Venkat.
Back to top
View user's profile Send private message
Lex
Developer


Joined: 16 Nov 2007
Posts: 30

PostPosted: Sun Jan 06, 2008 2:16 pm    Post subject: Reply with quote

You should check this page
http://www.theserverside.com/tt/articles/article.tss?l=HttpClient_FileUpload
Back to top
View user's profile Send private message
venkat
Senior Developer


Joined: 27 Nov 2007
Posts: 152
Location: India

PostPosted: Sun Jan 06, 2008 2:52 pm    Post subject: Reply with quote

Thanks for your reply Lex. I will check it out Smile

Regards,
Venkat Idea
Back to top
View user's profile Send private message
venkat
Senior Developer


Joined: 27 Nov 2007
Posts: 152
Location: India

PostPosted: Sun Jan 06, 2008 5:30 pm    Post subject: Reply with quote

Hi Lex,
i have gone through that article and below line is giving Error.
Java:
postMethod.setRequestBody(InputStream body)


In Android Documentation, They are saying that, This method is deprecated. use setRequestEntity(RequestEntity) Sad

Can you tell me any other way to do it?

Regards, Smile
Venkat.
Back to top
View user's profile Send private message
rmeph
Senior Developer


Joined: 10 Dec 2007
Posts: 109
Location: India

PostPosted: Mon Jan 07, 2008 3:15 pm    Post subject: Reply with quote

hi Lex,
do u know how simple file in android upload to web server???? Crying or Very sad
Back to top
View user's profile Send private message
cabernet1976
Senior Developer


Joined: 16 Nov 2007
Posts: 146
Location: China

PostPosted: Mon Jan 07, 2008 3:30 pm    Post subject: Reply with quote

Hi venkat,

I found this package ( Arrow https://jickr.dev.java.net/) has implemented a method to upload file to web server.
But I haven't tried it. Embarassed

_________________
EveryAlbum - http://www.anddev.org/viewtopic.php?p=7117#7117
Back to top
View user's profile Send private message Visit poster's website
venkat
Senior Developer


Joined: 27 Nov 2007
Posts: 152
Location: India

PostPosted: Mon Jan 07, 2008 3:49 pm    Post subject: Reply with quote

Hi cabernet,
Thanks for your reply, Is it correct link you have provided? Crying or Very sad I can't find any article related to upload files to web server. Sad

Thanks and Regards, Smile
Venkat.

_________________
Regards,
Venkat.
Back to top
View user's profile Send private message
rmeph
Senior Developer


Joined: 10 Dec 2007
Posts: 109
Location: India

PostPosted: Mon Jan 07, 2008 4:13 pm    Post subject: Reply with quote

i can't find any article in that link(https://jickr.dev.java.net).
plz forward correct link....... Crying or Very sad
Back to top
View user's profile Send private message
cabernet1976
Senior Developer


Joined: 16 Nov 2007
Posts: 146
Location: China

PostPosted: Tue Jan 08, 2008 2:14 am    Post subject: Reply with quote

Hi,

Can you download this file Arrow https://jickr.dev.java.net/files/documents/5444/39469/jickr-beta-02.zip Question
After unzip the downloading file, please find a file named PhotoUpload.java in direction 'example', I think you will find some thread there.

Please tell me, if you cannot download using that URL.

_________________
EveryAlbum - http://www.anddev.org/viewtopic.php?p=7117#7117
Back to top
View user's profile Send private message Visit poster's website
venkat
Senior Developer


Joined: 27 Nov 2007
Posts: 152
Location: India

PostPosted: Tue Jan 08, 2008 6:05 am    Post subject: Reply with quote

HI Cabernet,
i got zip file. I will go though this and let you know , If I have any query.


Thanks and Regards,
Venkat.

_________________
Regards,
Venkat.
Back to top
View user's profile Send private message
rmeph
Senior Developer


Joined: 10 Dec 2007
Posts: 109
Location: India

PostPosted: Tue Jan 08, 2008 11:38 am    Post subject: Reply with quote

also tell if i have any txt file..........
Back to top
View user's profile Send private message
Katharnavas
Senior Developer


Joined: 04 Dec 2007
Posts: 100
Location: India

PostPosted: Wed Jan 09, 2008 5:35 am    Post subject: Reply with quote

venkat wrote:
HI Cabernet,
i got zip file. I will go though this and let you know , If I have any query.


Thanks and Regards,
Venkat.



try this hope it will do

Java:
String exsistingFileName = "/sdcard/t1.mp3";

       String lineEnd = "\r\n";
       String twoHyphens = "--";
       String boundary =  "*****";
       try
       {
        //------------------ CLIENT REQUEST
       
            Log.e(Tag,"Inside second Method");

            FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );

        // open a URL connection to the Servlet

        URL url = new URL(urlString);


        // Open a HTTP connection to the URL

        conn = (HttpURLConnection) url.openConnection();

        // Allow Inputs
        conn.setDoInput(true);

        // Allow Outputs
        conn.setDoOutput(true);

        // Don't use a cached copy.
        conn.setUseCaches(false);

        // Use a post method.
        conn.setRequestMethod("POST");

        conn.setRequestProperty("Connection", "Keep-Alive");
       
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

        dos = new DataOutputStream( conn.getOutputStream() );

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
        dos.writeBytes(lineEnd);

        Log.e(Tag,"Headers are written");

        // create a buffer of maximum size

        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        // read file and write it into form...

        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0)
        {
         dos.write(buffer, 0, bufferSize);
         bytesAvailable = fileInputStream.available();
         bufferSize = Math.min(bytesAvailable, maxBufferSize);
         bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        // send multipart form data necesssary after file data...

        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // close streams
        Log.e(Tag,"File is written");
        fileInputStream.close();
        dos.flush();
        dos.close();


       }
       catch (MalformedURLException ex)
       {
            Log.e(Tag, "error: " + ex.getMessage(), ex);
       }

       catch (IOException ioe)
       {
            Log.e(Tag, "error: " + ioe.getMessage(), ioe);
       }


Last edited by Katharnavas on Wed Jan 09, 2008 1:18 pm; edited 1 time in total
Back to top
View user's profile Send private message Yahoo Messenger
rmeph
Senior Developer


Joined: 10 Dec 2007
Posts: 109
Location: India

PostPosted: Wed Jan 09, 2008 8:29 am    Post subject: Reply with quote

hi all,
any one tel me a simple file in android how to upload on server............
my file in
Java:
File f = new File(aDirectory.getAbsolutePath());

that upload on server using http protocal.............
Back to top
View user's profile Send private message
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials All times are GMT + 1 Hour
Goto page 1, 2, 3, 4  Next
Page 1 of 4

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


© 2007, Android Development Community
All rights reserved.
Powered by phpBB.