| Author |
Message |
Katharnavas Senior Developer

Joined: 04 Dec 2007 Posts: 100 Location: India
|
Posted: Thu Jan 10, 2008 1:23 pm Post subject: |
|
|
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 |
|
 |
|
|
 |
venkat Senior Developer

Joined: 27 Nov 2007 Posts: 152 Location: India
|
Posted: Thu Jan 10, 2008 2:29 pm Post subject: |
|
|
Dear Katharnavas. Thanks a lot. it works...
Regards,
Venkat..  _________________ Regards,
Venkat. |
|
| Back to top |
|
 |
venkat Senior Developer

Joined: 27 Nov 2007 Posts: 152 Location: India
|
Posted: Thu Jan 10, 2008 3:12 pm Post subject: |
|
|
Dear Katharnavas,
Is it possible to upload files using httpclient ??.
Thanks and Regards,
Venkat.  _________________ Regards,
Venkat. |
|
| Back to top |
|
 |
rmeph Senior Developer

Joined: 10 Dec 2007 Posts: 120 Location: India
|
Posted: Thu Jan 10, 2008 3:41 pm Post subject: |
|
|
thanks a lotssssssssssssss........it's working
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 |
|
 |
cabernet1976 Senior Developer

Joined: 16 Nov 2007 Posts: 154 Location: China
|
Posted: Fri Jan 11, 2008 2:47 am Post subject: |
|
|
Yesterday I uploaded a jpg file to web site successfully, I used jickr project's open source. _________________ Upload2Flickr's blog: http://upload2flickr.blogspot.com |
|
| Back to top |
|
 |
Katharnavas Senior Developer

Joined: 04 Dec 2007 Posts: 100 Location: India
|
Posted: Fri Jan 11, 2008 4:47 am Post subject: |
|
|
| 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 ? |
|
| Back to top |
|
 |
|
|
 |
venkat Senior Developer

Joined: 27 Nov 2007 Posts: 152 Location: India
|
Posted: Fri Jan 11, 2008 5:53 am Post subject: |
|
|
Thank you for your reply, Katharnavas. _________________ Regards,
Venkat. |
|
| Back to top |
|
 |
rmeph Senior Developer

Joined: 10 Dec 2007 Posts: 120 Location: India
|
Posted: Fri Jan 11, 2008 6:52 am Post subject: |
|
|
| rmeph wrote: | thanks a lotssssssssssssss........it's working
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 |
|
 |
venkat Senior Developer

Joined: 27 Nov 2007 Posts: 152 Location: India
|
Posted: Fri Jan 11, 2008 7:24 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
venkat Senior Developer

Joined: 27 Nov 2007 Posts: 152 Location: India
|
Posted: Fri Jan 11, 2008 7:34 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
rmeph Senior Developer

Joined: 10 Dec 2007 Posts: 120 Location: India
|
Posted: Fri Jan 11, 2008 7:41 am Post subject: |
|
|
| i know that is possible using ftp,but i wants use http........ |
|
| Back to top |
|
 |
venkat Senior Developer

Joined: 27 Nov 2007 Posts: 152 Location: India
|
Posted: Fri Jan 11, 2008 8:11 am Post subject: |
|
|
I will check it out. If i found i will inform to you...
Regards,
Venkat. _________________ Regards,
Venkat. |
|
| Back to top |
|
 |
rmeph Senior Developer

Joined: 10 Dec 2007 Posts: 120 Location: India
|
Posted: Fri Jan 11, 2008 8:12 am Post subject: |
|
|
| ok...thank you for reply....... |
|
| Back to top |
|
 |
venkat Senior Developer

Joined: 27 Nov 2007 Posts: 152 Location: India
|
Posted: Fri Jan 11, 2008 8:13 am Post subject: |
|
|
no problem..  _________________ Regards,
Venkat. |
|
| Back to top |
|
 |
raquibulbari Developer


Joined: 16 Dec 2007 Posts: 25 Location: dhaka,bangladesh
|
Posted: Thu Jan 24, 2008 5:23 pm Post subject: |
|
|
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
 _________________ Shimugool |
|
| Back to top |
|
 |
|
|
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.
|