Upload Files to Web Server

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Upload Files to Web Server

Postby venkat » Sat Jan 05, 2008 7:54 pm

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.

Syntax: [ Download ] [ Hide ]
Using php Syntax Highlighting
  1. <?php
  2. $target_path =  basename( $_FILES["uploadedfile"]["name"]);
  3. if(move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], $target_path)) {
  4. echo "The file ".basename( $_FILES["uploadedfile"]["name"])." has been uploaded";
  5. } else{
  6. echo "There was an error uploading the file, please try again!";
  7. }
  8. ?>
Parsed in 0.054 seconds, using GeSHi 1.0.8.4


Using above Php code :roll:, i can upload files from j2me. Can you any one tell me how to upload files from android. :?:

Thanks and Regards, :)
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Top

Re: Upload Files to Web Server

Postby ramonrabello » Sat Jan 05, 2008 8:59 pm

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: Select all
<?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 :roll:, i can upload files from j2me. Can you any one tell me how to upload files from android. :?:

Thanks and Regards, :)
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.
ramonrabello
Freshman
Freshman
 
Posts: 4
Joined: Sat Jan 05, 2008 8:51 pm

Postby venkat » Sat Jan 05, 2008 9:11 pm

Hi ramonrabello,
Thanks for your reply :), 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. :)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                                 try
  2.  
  3.                                 {
  4.  
  5.                                         HttpClient client = new HttpClient();
  6.  
  7.                                         HttpMethod method = new PostMethod("http://webserver.com/data.php?data=myData");
  8.  
  9.  
  10.  
  11.                                         client.executeMethod(method);
  12.  
  13.                                         a.setText("Ok"+method.getResponseBodyAsString());
  14.  
  15.                                         method.releaseConnection();
  16.  
  17.                                 }
  18.  
  19.                                 catch (Exception e)
  20.  
  21.                                 {
  22.  
  23.                                         a.setText("Error");
  24.  
  25.                                 }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


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

Thanks and regads, :)
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby Lex » Sun Jan 06, 2008 2:16 pm

Lex
Developer
Developer
 
Posts: 30
Joined: Fri Nov 16, 2007 11:03 pm

Postby venkat » Sun Jan 06, 2008 2:52 pm

Thanks for your reply Lex. I will check it out :)

Regards,
Venkat :idea:
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby venkat » Sun Jan 06, 2008 5:30 pm

Hi Lex,
i have gone through that article and below line is giving Error.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  postMethod.setRequestBody(InputStream body)
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


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

Can you tell me any other way to do it?

Regards, :)
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Top

Postby rmeph » Mon Jan 07, 2008 3:15 pm

hi Lex,
do u know how simple file in android upload to web server???? :cry:
rmeph
Senior Developer
Senior Developer
 
Posts: 121
Joined: Mon Dec 10, 2007 1:54 pm
Location: India

Postby cabernet1976 » Mon Jan 07, 2008 3:30 pm

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. :oops:
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby venkat » Mon Jan 07, 2008 3:49 pm

Hi cabernet,
Thanks for your reply, Is it correct link you have provided? :cry: I can't find any article related to upload files to web server. :(

Thanks and Regards, :)
Venkat.
Regards,
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby rmeph » Mon Jan 07, 2008 4:13 pm

i can't find any article in that link(https://jickr.dev.java.net).
plz forward correct link....... :cry:
rmeph
Senior Developer
Senior Developer
 
Posts: 121
Joined: Mon Dec 10, 2007 1:54 pm
Location: India

Postby cabernet1976 » Tue Jan 08, 2008 2:14 am

Hi,

Can you download this file :arrow: https://jickr.dev.java.net/files/docume ... eta-02.zip :?:
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.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby venkat » Tue Jan 08, 2008 6:05 am

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.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby rmeph » Tue Jan 08, 2008 11:38 am

also tell if i have any txt file..........
rmeph
Senior Developer
Senior Developer
 
Posts: 121
Joined: Mon Dec 10, 2007 1:54 pm
Location: India

Postby Katharnavas » Wed Jan 09, 2008 5:35 am

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

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String exsistingFileName = "/sdcard/t1.mp3";
  2.  
  3.  
  4.  
  5.           String lineEnd = "\r\n";
  6.  
  7.           String twoHyphens = "--";
  8.  
  9.           String boundary =  "*****";
  10.  
  11.           try
  12.  
  13.           {
  14.  
  15.            //------------------ CLIENT REQUEST
  16.  
  17.          
  18.  
  19.                   Log.e(Tag,"Inside second Method");
  20.  
  21.  
  22.  
  23.                   FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
  24.  
  25.  
  26.  
  27.            // open a URL connection to the Servlet
  28.  
  29.  
  30.  
  31.            URL url = new URL(urlString);
  32.  
  33.  
  34.  
  35.  
  36.  
  37.            // Open a HTTP connection to the URL
  38.  
  39.  
  40.  
  41.            conn = (HttpURLConnection) url.openConnection();
  42.  
  43.  
  44.  
  45.            // Allow Inputs
  46.  
  47.            conn.setDoInput(true);
  48.  
  49.  
  50.  
  51.            // Allow Outputs
  52.  
  53.            conn.setDoOutput(true);
  54.  
  55.  
  56.  
  57.            // Don't use a cached copy.
  58.  
  59.            conn.setUseCaches(false);
  60.  
  61.  
  62.  
  63.            // Use a post method.
  64.  
  65.            conn.setRequestMethod("POST");
  66.  
  67.  
  68.  
  69.            conn.setRequestProperty("Connection", "Keep-Alive");
  70.  
  71.          
  72.  
  73.            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
  74.  
  75.  
  76.  
  77.            dos = new DataOutputStream( conn.getOutputStream() );
  78.  
  79.  
  80.  
  81.            dos.writeBytes(twoHyphens + boundary + lineEnd);
  82.  
  83.            dos.writeBytes("Content-Disposition: form-data; name="uploadedfile";filename="" + exsistingFileName +""" + lineEnd);
  84.  
  85.            dos.writeBytes(lineEnd);
  86.  
  87.  
  88.  
  89.            Log.e(Tag,"Headers are written");
  90.  
  91.  
  92.  
  93.            // create a buffer of maximum size
  94.  
  95.  
  96.  
  97.            bytesAvailable = fileInputStream.available();
  98.  
  99.            bufferSize = Math.min(bytesAvailable, maxBufferSize);
  100.  
  101.            buffer = new byte[bufferSize];
  102.  
  103.  
  104.  
  105.            // read file and write it into form...
  106.  
  107.  
  108.  
  109.            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  110.  
  111.  
  112.  
  113.            while (bytesRead > 0)
  114.  
  115.            {
  116.  
  117.             dos.write(buffer, 0, bufferSize);
  118.  
  119.             bytesAvailable = fileInputStream.available();
  120.  
  121.             bufferSize = Math.min(bytesAvailable, maxBufferSize);
  122.  
  123.             bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  124.  
  125.            }
  126.  
  127.  
  128.  
  129.            // send multipart form data necesssary after file data...
  130.  
  131.  
  132.  
  133.            dos.writeBytes(lineEnd);
  134.  
  135.            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  136.  
  137.  
  138.  
  139.            // close streams
  140.  
  141.            Log.e(Tag,"File is written");
  142.  
  143.            fileInputStream.close();
  144.  
  145.            dos.flush();
  146.  
  147.            dos.close();
  148.  
  149.  
  150.  
  151.  
  152.  
  153.           }
  154.  
  155.           catch (MalformedURLException ex)
  156.  
  157.           {
  158.  
  159.                   Log.e(Tag, "error: " + ex.getMessage(), ex);
  160.  
  161.           }
  162.  
  163.  
  164.  
  165.           catch (IOException ioe)
  166.  
  167.           {
  168.  
  169.                   Log.e(Tag, "error: " + ioe.getMessage(), ioe);
  170.  
  171.           }
Parsed in 0.046 seconds, using GeSHi 1.0.8.4
Last edited by Katharnavas on Wed Jan 09, 2008 1:18 pm, edited 1 time in total.
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby rmeph » Wed Jan 09, 2008 8:29 am

hi all,
any one tel me a simple file in android how to upload on server............
my file in
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. File f = new File(aDirectory.getAbsolutePath());
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

that upload on server using http protocal.............
rmeph
Senior Developer
Senior Developer
 
Posts: 121
Joined: Mon Dec 10, 2007 1:54 pm
Location: India

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests