android - Why it returns Internal Server Error on httpUrlConnection? -
i'm developing application can send different files web server.also want send large files, in able need chunk files. when i'm sending files server nothing uploaded. don't know if there's error on how i'm sending files , gives error 500 (internal server error) on response.i don't think server problem because when i'm uploading file using multipartentity works when im using bufferedinputstream , dataoutputstream doesn't work. please me , tell what's wrong on code why can't send files. here's got far:
string samplefile = "storage/sdcard0/pictures/images/picture.jpg"; file mfile = new file(samplefile); int mychunksize = 2048 * 1024; final long size = mfile.length(); final long chunks = size < mychunksize? 1: (mfile.length() / mychunksize); int chunkid = 0; try { bufferedinputstream stream = new bufferedinputstream(new fileinputstream(mfile)); string lineend = "\r\n"; string twohyphens = "--"; string boundary = "-------------------------acebdf13572468";// random data (chunkid = 0; chunkid < chunks; chunkid++) { url url = new url(urlstring); // open http connection url httpurlconnection 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"); string encoded = base64.encodetostring((_username+":"+_password).getbytes(),base64.no_wrap); conn.setrequestproperty("authorization", "basic "+encoded); conn.setrequestproperty("connection", "keep-alive"); conn.setrequestproperty("content-type", "multipart/form-data;boundary="+boundary); dataoutputstream dos = new dataoutputstream( conn.getoutputstream() ); dos.writebytes(twohyphens + boundary + lineend); string param1 = ""+chunkid; string param2 = ""+chunks; string param3 = mfile.getname(); string param4 = samplefile; // send parameter #file dos.writebytes("content-disposition: form-data; name=\"fieldnamehere\";filename=\"" + param3 + "\"" + lineend); // filename name of file uploaded dos.writebytes("content-type: image/jpeg" + lineend); dos.writebytes(lineend); // send parameter #chunks dos.writebytes("content-disposition: form-data; name=\"chunk\"" + 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: " + param3.length() + lineend); dos.writebytes(lineend); dos.writebytes(param3 + lineend); dos.writebytes(twohyphens + boundary + lineend); byte[] buffer = new byte[mychunksize]; stream.read(buffer); dos.write(buffer); dos.writebytes(lineend); dos.writebytes(twohyphens + boundary + twohyphens + lineend); dos.flush(); dos.close(); } } catch (exception e) { log.e("error uploading files", e.tostring()); }
you can make use of this code. j2me class handle file uploads via http post multipart requests.
hope helps.
Comments
Post a Comment