android - Upload file does not show any error however does not upload the file -
hi trying upload video file android phone server using multiple methods. none of them seems work if now.
first of tried using normal multi-part upload uploading image , working well.
code snippet
public int uploadfilevideo(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(); runonuithread(new runnable() { public void run() { toast.maketext(gcmactivity.this, "file not found", toast.length_long).show(); } }); return 0; } else { try { // open url connection servlet fileinputstream fileinputstream = new fileinputstream(sourcefile); url url = new url("http://example.com/ccs-business/upload.php"); conn = (httpurlconnection) url.openconnection(); conn.setdoinput(true); conn.setdooutput(true); conn.setchunkedstreamingmode(0); conn.setusecaches(false); 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_video", 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("content-disposition: form-data; name=\"uploaded_video\";filename=\"" + filename +"\"" + lineend); dos.writebytes(lineend); bytesavailable = fileinputstream.available(); /* buffersize = math.min(bytesavailable, maxbuffersize); buffer = new byte[buffersize]; bytesread = fileinputstream.read(buffer, 0, buffersize); */ //buffersize = math.min(bytesavailable, maxbuffersize); //buffer = new byte[buffersize]; //byte byt[]=new byte[buffersize]; byte[] buf = new byte[ 4096 ]; int read = 0; while( ( read = fileinputstream.read( buf ) ) != -1 ) { dos.write( buf, 0, read ); } // 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() { toast.maketext(gcmactivity.this, "file upload complete.", toast.length_short).show(); dialog.dismiss(); } }); } fileinputstream.close(); dos.flush(); dos.close(); } catch (malformedurlexception ex) { dialog.dismiss(); ex.printstacktrace(); runonuithread(new runnable() { public void run() { toast.maketext(gcmactivity.this, "malformedurlexception", toast.length_short).show(); } }); log.v("onmessage", "error: " + ex.getmessage(), ex); } catch (exception e) { dialog.dismiss(); e.printstacktrace(); runonuithread(new runnable() { public void run() { toast.maketext(gcmactivity.this, "this file can not uploaded ", toast.length_short).show(); } }); log.v("onmessage", "exception : " + e.getmessage(), e); } dialog.dismiss(); return serverresponsecode; } // end else block }
it did not work, tried next procedure
code snippet
public void uploadwithhttpclient(string uri) { file myfile = new file(uri); try{ asynchttpclient client = new asynchttpclient(); requestparams params = new requestparams(); params.put("uploaded_video", myfile); client.post(this, "http://example.com/ccs-business/upload.php", params, new asynchttpresponsehandler() { @override public void onsuccess(int statuscode, header[] headers, byte[] responsebody) { dialog.dismiss(); log.v("onmessage", "success"); } @override public void onfailure(int statuscode, header[] headers, byte[] responsebody, throwable error) { log.v("onmessage", "failure"); dialog.dismiss(); } }); } catch(exception e) { dialog.dismiss(); log.v("onmessage", "exception " + e.tostring()); } }
then wrote class extends multipartentity in asynctask tried upload. files gets uploaded, not.
code snippet
public class custommultipartentity extends multipartentity { private final progresslistener listener; public custommultipartentity(final progresslistener listener) { super(); this.listener = listener; } public custommultipartentity(final httpmultipartmode mode, final progresslistener listener) { super(mode); this.listener = listener; } public custommultipartentity(httpmultipartmode mode, final string boundary, final charset charset, final progresslistener listener) { super(mode, boundary, charset); this.listener = listener; } @override public void writeto(final outputstream outstream) throws ioexception { super.writeto(new countingoutputstream(outstream, this.listener)); } public static interface progresslistener { void transferred(long num); } public static class countingoutputstream extends filteroutputstream { private final progresslistener listener; private long transferred; public countingoutputstream(final outputstream out, final progresslistener listener) { super(out); this.listener = listener; this.transferred = 0; } public void write(byte[] b, int off, int len) throws ioexception { out.write(b, off, len); this.transferred += len; this.listener.transferred(this.transferred); } public void write(int b) throws ioexception { out.write(b); this.transferred++; this.listener.transferred(this.transferred); } } }
aysnctask class is
private class httpmultipartpost extends asynctask<string, integer, string> { progressdialog pd; long totalsize; @override protected void onpreexecute() { pd = new progressdialog(gcmactivity.this); pd.setprogressstyle(progressdialog.style_horizontal); pd.setmessage("uploading video..."); pd.setcancelable(false); pd.show(); } @override protected string doinbackground(string... arg0) { httpclient httpclient = new defaulthttpclient(); httpcontext httpcontext = new basichttpcontext(); httppost httppost = new httppost("http://example.com/ccs-business/upload.php"); try { custommultipartentity multipartcontent = new custommultipartentity(new progresslistener() { @override public void transferred(long num) { publishprogress((int) ((num / (float) totalsize) * 100)); } }); // use filebody transfer image multipartcontent.addpart("uploaded_video", new filebody(new file(pathofpictoupload))); //multipartcontent.addpart(name, contentbody)d totalsize = multipartcontent.getcontentlength(); // send httppost.setentity(multipartcontent); httpresponse response = httpclient.execute(httppost, httpcontext); string serverresponse = entityutils.tostring(response.getentity()); return serverresponse; } catch (exception e) { system.out.println(e); } return null; } @override protected void onprogressupdate(integer... progress) { pd.setprogress((int) (progress[0])); } @override protected void onpostexecute(string ui) { pd.dismiss(); log.v("onmessage", "server response " + ui); } }
Comments
Post a Comment