java - Why I receive wrong chunk size after setting setChunkedStreamingMode? -
on client side ( android ) sent file using following code:
public string sendfiletoserver(string filename, string targeturl) { string response = "error"; log.e("image filename", filename); log.e("url", targeturl); httpurlconnection connection = null; dataoutputstream outputstream = null; // datainputstream inputstream = null; string pathtoourfile = filename; string urlserver = targeturl; string lineend = "\r\n"; string twohyphens = "--"; string boundary = "*****"; dateformat df = new simpledateformat("yyyy_mm_dd_hh:mm:ss"); int bytesread, bytesavailable, buffersize; byte[] buffer; int maxbuffersize = 1 * 1024; try { fileinputstream fileinputstream = new fileinputstream(new file( pathtoourfile)); url url = new url(urlserver); connection = (httpurlconnection) url.openconnection(); // allow inputs & outputs connection.setdoinput(true); connection.setdooutput(true); connection.setusecaches(false); connection.setchunkedstreamingmode(1024); //connection.set // enable post method connection.setrequestmethod("post"); connection.setrequestproperty("connection", "keep-alive"); connection.setrequestproperty("content-type", "multipart/form-data;boundary=" + boundary); outputstream = new dataoutputstream(connection.getoutputstream()); outputstream.writebytes(twohyphens + boundary + lineend); string connstr = null; connstr = "content-disposition: form-data; name=\"uploadedfile\";filename=\"" + "uploadedfile.log" + "\"" + lineend; log.i("connstr", connstr); outputstream.writebytes(connstr); outputstream.writebytes(lineend); bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); buffer = new byte[buffersize]; // read file bytesread = fileinputstream.read(buffer, 0, buffersize); log.e("image length", bytesavailable + ""); try { while (bytesread > 0) { try { outputstream.write(buffer, 0, buffersize); } catch (outofmemoryerror e) { e.printstacktrace(); response = "outofmemoryerror"; return response; } bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); bytesread = fileinputstream.read(buffer, 0, buffersize); } } catch (exception e) { e.printstacktrace(); response = "error"; return response; } outputstream.writebytes(lineend); outputstream.writebytes(twohyphens + boundary + twohyphens + lineend); // responses server (code , message) int serverresponsecode = connection.getresponsecode(); string serverresponsemessage = connection.getresponsemessage(); log.i("server response code ", "" + serverresponsecode); log.i("server response message", serverresponsemessage); if (serverresponsecode == 200) { response = "true"; } string cdate = null; date servertime = new date(connection.getdate()); try { cdate = df.format(servertime); } catch (exception e) { e.printstacktrace(); log.e("date exception", e.getmessage() + " parse exception"); } log.i("server response time", cdate + ""); filename = cdate + filename.substring(filename.lastindexof("."), filename.length()); log.i("file name in server : ", filename); fileinputstream.close(); outputstream.flush(); outputstream.close(); outputstream = null; } catch (exception ex) { // exception handling response = "error"; log.e("send file exception", ex.getmessage() + ""); ex.printstacktrace(); } return response; }
on server side, implemented simple socketserver using java
import java.io.*; import java.net.*; public class server{ public static void main(string args[]){ try{ serversocket serversocket=new serversocket(8081); while(true){ socket socket=serversocket.accept(); system.out.println("just connected "+ socket.getremotesocketaddress()); bufferedinputstream in=new bufferedinputstream(socket.getinputstream()); int x; while((x=in.read())!=-1){ system.out.print((char)x); } } }catch(ioexception e){ } } }
i trying check being sent android side. surprise, surely receive chunked data chunk header contains wrong chunk size. content size of follows after chunk size different chunk size mentioned.
could please tell me doing wrong ?
Comments
Post a Comment