java - Send image with HttpUrlConnection by chunks and other parameters -


the thing i'm trying upload image server. image has uploaded chunks of 256kb , need pass chunks count , id every call. can total number of chunks upload , i'm using bufferedinputstream chunks bytes. when finish upload chunks image showed corrupted.

my code far:

int chunksize = 255 * 1024; final long size = mfile.length(); final long chunks = mfile.length() < chunksize? 1: (mfile.length() / chunksize);  int chunkid = 0;  bufferedinputstream stream = new bufferedinputstream(new fileinputstream(mfile));  string lineend = "\r\n"; string twohyphens = "--"; string boundary =  "rqdzaaihjq7xp1kjraqf";// random data  (chunkid = 0; chunkid < chunks; chunkid++) {       url url = new url(urlstring);      // open http connection url      conn = (httpurlconnection) url.openconnection();       conn.setreadtimeout(20000 /* milliseconds */);      conn.setconnecttimeout(20000 /* milliseconds */);        // allow inputs      conn.setdoinput(true);      // allow outputs      conn.setdooutput(true);      // don't use cached copy.      conn.setusecaches(false);      // use post method.      conn.setrequestmethod("post");      conn.setrequestproperty("connection", "keep-alive");       conn.setrequestproperty("content-type", "multipart/form-data;boundary="+boundary);      dos = new dataoutputstream( conn.getoutputstream() );      dos.writebytes(twohyphens + boundary + lineend);       string param1 = ""+chunkid;      string param2 = ""+chunks;      string param3 = mfile.getname();      // every param     dos.writebytes("content-disposition: form-data; name=\"chunk\"" + lineend);     dos.writebytes("content-type: text/plain; charset=utf-8" + lineend);     dos.writebytes("content-length: " + param1.length() + lineend);     dos.writebytes(lineend);     dos.writebytes(param1 + lineend);     dos.writebytes(twohyphens + boundary + lineend);      // send parameter #chunks     dos.writebytes("content-disposition: form-data; name=\"chunks\"" + lineend);     dos.writebytes("content-type: text/plain; charset=utf-8" + lineend);     dos.writebytes("content-length: " + param2.length() + lineend);     dos.writebytes(lineend);     dos.writebytes(param2 + lineend);     dos.writebytes(twohyphens + boundary + lineend);       // send parameter #name     dos.writebytes("content-disposition: form-data; name=\"name\"" + lineend);     dos.writebytes("content-type: text/plain; charset=utf-8" + lineend);     dos.writebytes("content-length: " + param4.length() + lineend);     dos.writebytes(lineend);     dos.writebytes(param3 + lineend);     dos.writebytes(twohyphens + boundary + lineend);      // send parameter #file     dos.writebytes("content-disposition: form-data; name=\"file\";filename=\"" + param4 + "\"" + lineend); // filename name of file uploaded      dos.writebytes("content-type: image/jpeg" + lineend);     dos.writebytes(lineend);      byte[] buffer = new byte[chunksize];      stream.skip(chunkid * chunksize);     stream.read(buffer);      // dos.write(buffer, 0, buffersize);     dos.write(buffer);       dos.writebytes(lineend);     dos.writebytes(twohyphens + boundary + twohyphens + lineend);     dos.flush();     dos.close();   // read response...  } 

many thanks!

well,

i solved issue. deleted following line:

stream.skip(chunkid * chunksize); 

i skipping several chunks of stream :). sorry bad.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -