Upload Files to Web Server

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

Postby Katharnavas » Thu Jan 10, 2008 1:23 pm

Hi,
can you try the same code (doFileUpload() and upload.php )both client and server with your url and let me know whether that code worked for you? Please use upload.php code and create a folder named uploads in your localhost directory.
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Top

Postby venkat » Thu Jan 10, 2008 2:29 pm

Dear Katharnavas. Thanks a lot. it works... :) :D :) :D


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

Postby venkat » Thu Jan 10, 2008 3:12 pm

Dear Katharnavas,:)
Is it possible to upload files using httpclient ??.

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 » Thu Jan 10, 2008 3:41 pm

thanks a lotssssssssssssss........it's working :D


i try "PUT" method in client side to upload file server.....but without using server side coding

use code

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. HttpURLConnection conn = null;
  2.  
  3.                          DataOutputStream dos = null;
  4.  
  5.                          DataInputStream inStream = null;
  6.  
  7.  
  8.  
  9.  
  10.  
  11.                          String exsistingFileName = aDirectory.getAbsolutePath();
  12.  
  13.                          // Is this the place are you doing something wrong.
  14.  
  15.  
  16.  
  17.                          String lineEnd = "\r\n";
  18.  
  19.                          String twoHyphens = "--";
  20.  
  21.                          String boundary = "*****";
  22.  
  23.  
  24.  
  25.  
  26.  
  27.                          int bytesRead, bytesAvailable, bufferSize;
  28.  
  29.  
  30.  
  31.                          byte[] buffer;
  32.  
  33.  
  34.  
  35.                          int maxBufferSize = 1*1024*1024;
  36.  
  37.  
  38.  
  39.  
  40.  
  41.                          String responseFromServer = "";
  42.  
  43.  
  44.  
  45.                          String urlString = "http://www.isol.co.in/isol_final/";
  46.  
  47.  
  48.  
  49.  
  50.  
  51.                          try
  52.  
  53.                          {
  54.  
  55.                          //------------------ CLIENT REQUEST
  56.  
  57.  
  58.  
  59.                          Log.e("fileupload","Inside second Method");
  60.  
  61.  
  62.  
  63.                          FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
  64.  
  65.  
  66.  
  67.                          // open a URL connection to the Servlet
  68.  
  69.  
  70.  
  71.                          URL url = new URL(urlString);
  72.  
  73.  
  74.  
  75.  
  76.  
  77.                          // Open a HTTP connection to the URL
  78.  
  79.  
  80.  
  81.                          conn = (HttpURLConnection) url.openConnection();
  82.  
  83.  
  84.  
  85.                          // Allow Inputs
  86.  
  87.                          conn.setDoInput(true);
  88.  
  89.  
  90.  
  91.                          // Allow Outputs
  92.  
  93.                          conn.setDoOutput(true);
  94.  
  95.  
  96.  
  97.                          // Don't use a cached copy.
  98.  
  99.                          conn.setUseCaches(false);
  100.  
  101.  
  102.  
  103.                          // Use a post method.
  104.  
  105.                          conn.setRequestMethod("PUT");
  106.  
  107.  
  108.  
  109.                          conn.setRequestProperty("Connection", "Keep-Alive");
  110.  
  111.  
  112.  
  113.                          conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
  114.  
  115.  
  116.  
  117.                          dos = new DataOutputStream( conn.getOutputStream() );
  118.  
  119.  
  120.  
  121.                          dos.writeBytes(twoHyphens + boundary + lineEnd);
  122.  
  123.                          dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
  124.  
  125.                          dos.writeBytes(lineEnd);
  126.  
  127.  
  128.  
  129.                          Log.e("MediaPlayer","Headers are written");
  130.  
  131.  
  132.  
  133.                          // create a buffer of maximum size
  134.  
  135.  
  136.  
  137.                          bytesAvailable = fileInputStream.available();
  138.  
  139.                          bufferSize = Math.min(bytesAvailable, maxBufferSize);
  140.  
  141.                          buffer = new byte[bufferSize];
  142.  
  143.  
  144.  
  145.                          // read file and write it into form...
  146.  
  147.  
  148.  
  149.                          bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  150.  
  151.  
  152.  
  153.                          while (bytesRead > 0)
  154.  
  155.                          {
  156.  
  157.                          dos.write(buffer, 0, bufferSize);
  158.  
  159.                          bytesAvailable = fileInputStream.available();
  160.  
  161.                          bufferSize = Math.min(bytesAvailable, maxBufferSize);
  162.  
  163.                          bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  164.  
  165.                          }
  166.  
  167.  
  168.  
  169.                          // send multipart form data necesssary after file data...
  170.  
  171.  
  172.  
  173.                          dos.writeBytes(lineEnd);
  174.  
  175.                          dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  176.  
  177.  
  178.  
  179.                          // close streams
  180.  
  181.                          Log.e("fileupload","File is written");
  182.  
  183.                          fileInputStream.close();
  184.  
  185.                          dos.flush();
  186.  
  187.                          dos.close();
  188.  
  189.  
  190.  
  191.  
  192.  
  193.                          }
  194.  
  195.                          catch (MalformedURLException ex)
  196.  
  197.                          {
  198.  
  199.                          Log.e("fileupload", "error: " + ex.getMessage(), ex);
  200.  
  201.                          }
  202.  
  203.  
  204.  
  205.                          catch (IOException ioe)
  206.  
  207.                          {
  208.  
  209.                          Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
  210.  
  211.                          }
  212.  
  213.  
  214.  
  215.  
  216.  
  217.                          //------------------ read the SERVER RESPONSE
  218.  
  219.  
  220.  
  221.  
  222.  
  223.                          try {
  224.  
  225.                                   BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  226.  
  227.                               String line;
  228.  
  229.                               while ((line = rd.readLine()) != null) {                
  230.  
  231.                                   showAlert("Dialoge Box", "Message: " + line,"OK", false);
  232.  
  233.                               }
  234.  
  235.                               rd.close();
  236.  
  237.  
  238.  
  239.                          }
  240.  
  241.                          catch (IOException ioex){
  242.  
  243.                          Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
  244.  
  245.                          }
Parsed in 0.050 seconds, using GeSHi 1.0.8.4


it's shoe exception is
E/fileupload<764>:error: http"//www.isol.co.in/isol_final/<HTTP 405- Method Not Allowed>
E/fileupload<764>:java.io.FileNotExceptionException:http"//www.isol.co.in/isol_final/<HTTP 405- Method Not Allowed>
E/fileupload<764>: at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:885)
E/fileupload<764>: at org.anddev.android.filebowser.AndroidfileBowser$1.onClick<AndroidfileBowser.java:192>
E/fileupload<764>: at android.app.AlertDialog$2.handleMessage<AlertDialog.java:437>
E/fileupload<764>: st android.os.Handler.dispatchMessage<Hanlder.java:80>
E/fileupload(764): at android.os.Looper.loop(Looper.java:71)
E/MediaPlayer(764): at android.app.ActivityThread.main(ActivityThread.java:2506)
E/fileupload(764): at java.lang.reflect.Method.invokeNative(Native Method)
E/fileupload(764): at java.lang.reflect.Method.invoke(Method.java:380)
E/MediaPlayer(764): at android.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1170)
E/fileupload(764): at android.os.ZygoteInit.main(ZygoteInit.java:1121)
E/fileupload(764): at android.dalvik.NativeStart.main(Native Method)


plz tel me why it's show that exception??
rmeph
Senior Developer
Senior Developer
 
Posts: 121
Joined: Mon Dec 10, 2007 1:54 pm
Location: India

Postby cabernet1976 » Fri Jan 11, 2008 2:47 am

Yesterday I uploaded a jpg file to web site successfully, I used jickr project's open source.
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 Katharnavas » Fri Jan 11, 2008 4:47 am

venkat wrote:Dear Katharnavas,:)
Is it possible to upload files using httpclient ??.

Thanks and Regards,
Venkat. :)


I did make a single try but could not succeed .. Busy with some other works so left it out.. If i came out successful i will share that ?
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Top

Postby venkat » Fri Jan 11, 2008 5:53 am

Thank you for your reply, Katharnavas.
Regards,
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby rmeph » Fri Jan 11, 2008 6:52 am

rmeph wrote:thanks a lotssssssssssssss........it's working :D


i try "PUT" method in client side to upload file server.....but without using server side coding

use code

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. HttpURLConnection conn = null;
  2.                          DataOutputStream dos = null;
  3.                          DataInputStream inStream = null;
  4.  
  5.  
  6.                          String exsistingFileName = aDirectory.getAbsolutePath();
  7.                          // Is this the place are you doing something wrong.
  8.  
  9.                          String lineEnd = "\r\n";
  10.                          String twoHyphens = "--";
  11.                          String boundary = "*****";
  12.  
  13.  
  14.                          int bytesRead, bytesAvailable, bufferSize;
  15.  
  16.                          byte[] buffer;
  17.  
  18.                          int maxBufferSize = 1*1024*1024;
  19.  
  20.  
  21.                          String responseFromServer = "";
  22.  
  23.                          String urlString = "http://www.isol.co.in/isol_final/";
  24.  
  25.  
  26.                          try
  27.                          {
  28.                          //------------------ CLIENT REQUEST
  29.  
  30.                          Log.e("fileupload","Inside second Method");
  31.  
  32.                          FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
  33.  
  34.                          // open a URL connection to the Servlet
  35.  
  36.                          URL url = new URL(urlString);
  37.  
  38.  
  39.                          // Open a HTTP connection to the URL
  40.  
  41.                          conn = (HttpURLConnection) url.openConnection();
  42.  
  43.                          // Allow Inputs
  44.                          conn.setDoInput(true);
  45.  
  46.                          // Allow Outputs
  47.                          conn.setDoOutput(true);
  48.  
  49.                          // Don't use a cached copy.
  50.                          conn.setUseCaches(false);
  51.  
  52.                          // Use a post method.
  53.                          conn.setRequestMethod("PUT");
  54.  
  55.                          conn.setRequestProperty("Connection", "Keep-Alive");
  56.  
  57.                          conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
  58.  
  59.                          dos = new DataOutputStream( conn.getOutputStream() );
  60.  
  61.                          dos.writeBytes(twoHyphens + boundary + lineEnd);
  62.                          dos.writeBytes("Content-Disposition: form-data; name="uploadedfile";filename="" + exsistingFileName +""" + lineEnd);
  63.                          dos.writeBytes(lineEnd);
  64.  
  65.                          Log.e("MediaPlayer","Headers are written");
  66.  
  67.                          // create a buffer of maximum size
  68.  
  69.                          bytesAvailable = fileInputStream.available();
  70.                          bufferSize = Math.min(bytesAvailable, maxBufferSize);
  71.                          buffer = new byte[bufferSize];
  72.  
  73.                          // read file and write it into form...
  74.  
  75.                          bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  76.  
  77.                          while (bytesRead > 0)
  78.                          {
  79.                          dos.write(buffer, 0, bufferSize);
  80.                          bytesAvailable = fileInputStream.available();
  81.                          bufferSize = Math.min(bytesAvailable, maxBufferSize);
  82.                          bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  83.                          }
  84.  
  85.                          // send multipart form data necesssary after file data...
  86.  
  87.                          dos.writeBytes(lineEnd);
  88.                          dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  89.  
  90.                          // close streams
  91.                          Log.e("fileupload","File is written");
  92.                          fileInputStream.close();
  93.                          dos.flush();
  94.                          dos.close();
  95.  
  96.  
  97.                          }
  98.                          catch (MalformedURLException ex)
  99.                          {
  100.                          Log.e("fileupload", "error: " + ex.getMessage(), ex);
  101.                          }
  102.  
  103.                          catch (IOException ioe)
  104.                          {
  105.                          Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
  106.                          }
  107.  
  108.  
  109.                          //------------------ read the SERVER RESPONSE
  110.  
  111.  
  112.                          try {
  113.                                   BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  114.                               String line;
  115.                               while ((line = rd.readLine()) != null) {                
  116.                                   showAlert("Dialoge Box", "Message: " + line,"OK", false);
  117.                               }
  118.                               rd.close();
  119.  
  120.                          }
  121.                          catch (IOException ioex){
  122.                          Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
  123.                          }
Parsed in 0.050 seconds, using GeSHi 1.0.8.4


it's shoe exception is
E/fileupload<764>:error: http"//www.isol.co.in/isol_final/<HTTP 405- Method Not Allowed>
E/fileupload<764>:java.io.FileNotExceptionException:http"//www.isol.co.in/isol_final/<HTTP 405- Method Not Allowed>
E/fileupload<764>: at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:885)
E/fileupload<764>: at org.anddev.android.filebowser.AndroidfileBowser$1.onClick<AndroidfileBowser.java:192>
E/fileupload<764>: at android.app.AlertDialog$2.handleMessage<AlertDialog.java:437>
E/fileupload<764>: st android.os.Handler.dispatchMessage<Hanlder.java:80>
E/fileupload(764): at android.os.Looper.loop(Looper.java:71)
E/MediaPlayer(764): at android.app.ActivityThread.main(ActivityThread.java:2506)
E/fileupload(764): at java.lang.reflect.Method.invokeNative(Native Method)
E/fileupload(764): at java.lang.reflect.Method.invoke(Method.java:380)
E/MediaPlayer(764): at android.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1170)
E/fileupload(764): at android.os.ZygoteInit.main(ZygoteInit.java:1121)
E/fileupload(764): at android.dalvik.NativeStart.main(Native Method)


plz tel me why it's show that exception??



that is possible??????
rmeph
Senior Developer
Senior Developer
 
Posts: 121
Joined: Mon Dec 10, 2007 1:54 pm
Location: India

Postby venkat » Fri Jan 11, 2008 7:24 am

Hi rmeph :),
i don't thing so it is possible to upload files to server with out using server side coding like php, jsp, etc..


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

Postby venkat » Fri Jan 11, 2008 7:34 am

Hi rmeph,
If you want to upload files to server without server side code, you wants to use fTP server.
take a look hereFTP Client library. I hope it will help to you. I have n't tested yet.

feel free to ask questions,
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 » Fri Jan 11, 2008 7:41 am

i know that is possible using ftp,but i wants use http........
rmeph
Senior Developer
Senior Developer
 
Posts: 121
Joined: Mon Dec 10, 2007 1:54 pm
Location: India

Postby venkat » Fri Jan 11, 2008 8:11 am

I will check it out. If i found i will inform to you...

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

Postby rmeph » Fri Jan 11, 2008 8:12 am

ok...thank you for reply.......
rmeph
Senior Developer
Senior Developer
 
Posts: 121
Joined: Mon Dec 10, 2007 1:54 pm
Location: India

Postby venkat » Fri Jan 11, 2008 8:13 am

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

Postby raquibulbari » Thu Jan 24, 2008 5:23 pm

Katharnavas thanks man , your code came in great help for me, the whole day i was trying to upload image, the code was about to be same like you , at least your code worked, thanks man, i was just getting frustrated on me

:D
Shimugool
User avatar
raquibulbari
Developer
Developer
 
Posts: 25
Joined: Sun Dec 16, 2007 6:31 am
Location: dhaka,bangladesh

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: Google Feedfetcher and 6 guests