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 Previous  1, 2, 3, 4  Next
 
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials
Author Message
Katharnavas
Senior Developer


Joined: 04 Dec 2007
Posts: 100
Location: India

PostPosted: Thu Jan 10, 2008 1:23 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Yahoo Messenger
venkat
Senior Developer


Joined: 27 Nov 2007
Posts: 152
Location: India

PostPosted: Thu Jan 10, 2008 2:29 pm    Post subject: Reply with quote

Dear Katharnavas. Thanks a lot. it works... Smile Very Happy Smile Very Happy


Regards, Very Happy
Venkat.. Smile Very Happy Smile

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


Joined: 27 Nov 2007
Posts: 152
Location: India

PostPosted: Thu Jan 10, 2008 3:12 pm    Post subject: Reply with quote

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

Thanks and Regards,
Venkat. Smile

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


Joined: 10 Dec 2007
Posts: 109
Location: India

PostPosted: Thu Jan 10, 2008 3:41 pm    Post subject: Reply with quote

thanks a lotssssssssssssss........it's working Very Happy


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

use code

Java:
HttpURLConnection conn = null;
                          DataOutputStream dos = null;
                          DataInputStream inStream = null;


                          String exsistingFileName = aDirectory.getAbsolutePath();
                          // Is this the place are you doing something wrong.

                          String lineEnd = "\r\n";
                          String twoHyphens = "--";
                          String boundary = "*****";


                          int bytesRead, bytesAvailable, bufferSize;

                          byte[] buffer;

                          int maxBufferSize = 1*1024*1024;


                          String responseFromServer = "";

                          String urlString = "http://www.isol.co.in/isol_final/";


                          try
                          {
                          //------------------ CLIENT REQUEST

                          Log.e("fileupload","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("PUT");

                          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("MediaPlayer","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("fileupload","File is written");
                          fileInputStream.close();
                          dos.flush();
                          dos.close();


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

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


                          //------------------ read the SERVER RESPONSE


                          try {
                                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                              String line;
                              while ((line = rd.readLine()) != null) {                
                                  showAlert("Dialoge Box", "Message: " + line,"OK", false);
                              }
                              rd.close();

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


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??
Back to top
View user's profile Send private message
cabernet1976
Senior Developer


Joined: 16 Nov 2007
Posts: 147
Location: China

PostPosted: Fri Jan 11, 2008 2:47 am    Post subject: Reply with quote

Yesterday I uploaded a jpg file to web site successfully, I used jickr project's open source.
_________________
EveryAlbum - http://www.anddev.org/viewtopic.php?p=7117#7117
Back to top
View user's profile Send private message Visit poster's website
Katharnavas
Senior Developer


Joined: 04 Dec 2007
Posts: 100
Location: India

PostPosted: Fri Jan 11, 2008 4:47 am    Post subject: Reply with quote

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

Thanks and Regards,
Venkat. Smile


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 ?
Back to top
View user's profile Send private message Yahoo Messenger
venkat
Senior Developer


Joined: 27 Nov 2007
Posts: 152
Location: India

PostPosted: Fri Jan 11, 2008 5:53 am    Post subject: Reply with quote

Thank you for your reply, Katharnavas.
_________________
Regards,
Venkat.
Back to top
View user's profile Send private message
rmeph
Senior Developer


Joined: 10 Dec 2007
Posts: 109
Location: India

PostPosted: Fri Jan 11, 2008 6:52 am    Post subject: Reply with quote

rmeph wrote:
thanks a lotssssssssssssss........it's working Very Happy


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

use code

Java:
HttpURLConnection conn = null;
                          DataOutputStream dos = null;
                          DataInputStream inStream = null;


                          String exsistingFileName = aDirectory.getAbsolutePath();
                          // Is this the place are you doing something wrong.

                          String lineEnd = "\r\n";
                          String twoHyphens = "--";
                          String boundary = "*****";


                          int bytesRead, bytesAvailable, bufferSize;

                          byte[] buffer;

                          int maxBufferSize = 1*1024*1024;


                          String responseFromServer = "";

                          String urlString = "http://www.isol.co.in/isol_final/";


                          try
                          {
                          //------------------ CLIENT REQUEST

                          Log.e("fileupload","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("PUT");

                          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("MediaPlayer","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("fileupload","File is written");
                          fileInputStream.close();
                          dos.flush();
                          dos.close();


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

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


                          //------------------ read the SERVER RESPONSE


                          try {
                                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                              String line;
                              while ((line = rd.readLine()) != null) {                
                                  showAlert("Dialoge Box", "Message: " + line,"OK", false);
                              }
                              rd.close();

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


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??????
Back to top
View user's profile Send private message
venkat
Senior Developer


Joined: 27 Nov 2007
Posts: 152
Location: India

PostPosted: Fri Jan 11, 2008 7:24 am    Post subject: Reply with quote

Hi rmeph Smile,
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.
Back to top
View user's profile Send private message
venkat
Senior Developer


Joined: 27 Nov 2007
Posts: 152
Location: India

PostPosted: Fri Jan 11, 2008 7:34 am    Post subject: Reply with quote

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,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: Fri Jan 11, 2008 7:41 am    Post subject: Reply with quote

i know that is possible using ftp,but i wants use http........
Back to top
View user's profile Send private message
venkat
Senior Developer


Joined: 27 Nov 2007
Posts: 152
Location: India

PostPosted: Fri Jan 11, 2008 8:11 am    Post subject: Reply with quote

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

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: Fri Jan 11, 2008 8:12 am    Post subject: Reply with quote

ok...thank you for reply.......
Back to top
View user's profile Send private message
venkat
Senior Developer


Joined: 27 Nov 2007
Posts: 152
Location: India

PostPosted: Fri Jan 11, 2008 8:13 am    Post subject: Reply with quote

no problem.. Smile
_________________
Regards,
Venkat.
Back to top
View user's profile Send private message
raquibulbari
Developer


Joined: 16 Dec 2007
Posts: 25
Location: dhaka,bangladesh

PostPosted: Thu Jan 24, 2008 5:23 pm    Post subject: Reply with quote

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

Very Happy

_________________
Shimugool
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials All times are GMT + 1 Hour
Goto page Previous  1, 2, 3, 4  Next
Page 3 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.