ftp client - Unable to Upload file from SdCard to FTP Server using android -
i'm newbie in android , want upload file sdcard ftp server. followed this tutorial
it doesn't work me , got warning in logcat:
05-11 17:06:22.226: i/system.out(13571): [cds]rx timeout:0 05-11 17:06:23.994: i/uploadfile(13571): http response : bad request: 400 05-11 17:06:23.994: d/dalvikvm(13571): threadid=12: exiting 05-11 17:06:23.994: d/dalvikvm(13571): threadid=12: bye!
could me please? code: `
import java.io.dataoutputstream; import java.io.file; import java.io.fileinputstream; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import android.app.activity; import android.app.progressdialog; import android.os.bundle; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; import android.widget.toast; public class uploadtoserver extends activity { textview messagetext; button uploadbutton; int serverresponsecode = 0; progressdialog dialog = null; string uploadserveruri = null; /********** file path *************/ final string uploadfilepath = "/storage/sdcard0/testftp/"; final string uploadfilename = "map.png"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_upload_to_server); uploadbutton = (button)findviewbyid(r.id.uploadbutton); messagetext = (textview)findviewbyid(r.id.messagetext); messagetext.settext("uploading file path :- '/mnt/sdcard/"+uploadfilename+"'"); /************* php script path ****************/ uploadserveruri = "http://servername/media/upload.php"; uploadbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { dialog = progressdialog.show(uploadtoserver.this, "", "uploading file...", true); new thread(new runnable() { public void run() { runonuithread(new runnable() { public void run() { messagetext.settext("uploading started....."); } }); uploadfile(uploadfilepath + "" + uploadfilename); } }).start(); } }); } public int uploadfile(string sourcefileuri) { string filename = sourcefileuri; httpurlconnection conn = null; dataoutputstream dos = null; string lineend = "\r\n"; string twohyphens = "--"; string boundary = "*****"; int bytesread, bytesavailable, buffersize; byte[] buffer; int maxbuffersize = 1 * 1024 * 1024; file sourcefile = new file(sourcefileuri); if (!sourcefile.isfile()) { dialog.dismiss(); log.e("uploadfile", "source file not exist :" +uploadfilepath + "" + uploadfilename); runonuithread(new runnable() { public void run() { messagetext.settext("source file not exist :" +uploadfilepath + "" + uploadfilename); } }); return 0; } else { try { // open url connection servlet fileinputstream fileinputstream = new fileinputstream(sourcefile); url url = new url(uploadserveruri); // open http connection url conn = (httpurlconnection) url.openconnection(); conn.setdoinput(true); // allow inputs conn.setdooutput(true); // allow outputs conn.setusecaches(false); // don't use cached copy conn.setrequestmethod("post"); conn.setrequestproperty("connection", "keep-alive"); conn.setrequestproperty("enctype", "multipart/form-data"); conn.setrequestproperty("content-type", "multipart/form-data;boundary=" + boundary); conn.setrequestproperty("uploaded_file", filename); dos = new dataoutputstream(conn.getoutputstream()); dos.writebytes(twohyphens + boundary + lineend); dos.writebytes("content-disposition: form-data; name="+"uploaded_file"+";filename="+ filename + "" + lineend); dos.writebytes(lineend); // create buffer of maximum size bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); buffer = new byte[buffersize]; // read file , write 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); // responses server (code , message) serverresponsecode = conn.getresponsecode(); string serverresponsemessage = conn.getresponsemessage(); log.i("uploadfile", "http response : " + serverresponsemessage + ": " + serverresponsecode); if(serverresponsecode == 200){ runonuithread(new runnable() { public void run() { string msg = "file upload completed.\n\n see uploaded file here : \n\n" +" http://www.androidexample.com/media/uploads/" +uploadfilename; messagetext.settext(msg); toast.maketext(uploadtoserver.this, "file upload complete.", toast.length_short).show(); } }); } //close streams // fileinputstream.close(); dos.flush(); dos.close(); } catch (malformedurlexception ex) { dialog.dismiss(); ex.printstacktrace(); runonuithread(new runnable() { public void run() { messagetext.settext("malformedurlexception exception : check script url."); toast.maketext(uploadtoserver.this, "malformedurlexception", toast.length_short).show(); } }); log.e("upload file server", "error: " + ex.getmessage(), ex); } catch (exception e) { dialog.dismiss(); e.printstacktrace(); runonuithread(new runnable() { public void run() { messagetext.settext("got exception : see logcat "); toast.maketext(uploadtoserver.this, "got exception : see logcat ", toast.length_short).show(); } }); log.e("upload file server exception", "exception : " + e.getmessage(), e); } dialog.dismiss(); return serverresponsecode; } // end else block }
}`
quote http 400 "bad request error":
the web server (running web site) thinks data stream sent client (e.g. web browser or our checkupdown robot) 'malformed' i.e. did not respect http protocol completely. web server unable understand request , process it.
malformed means request wasn't written properly. mean part:
dos.writebytes(twohyphens + boundary + lineend); dos.writebytes("content-disposition: form-data; name="+"uploaded_file"+";filename="+ filename + "" + lineend); dos.writebytes(lineend); // sending file... dos.writebytes(lineend); dos.writebytes(twohyphens + boundary + twohyphens + lineend);
to fix error suggest making sure things above right, because server isn't understanding them. there tutorial uploading files here. may not android i've seen uses classes included in android api.
Comments
Post a Comment