java - Why I'm getting error response from server when trying to upload file using HttpURLConnection -


i'm trying upload file server(not mine) think did needs please can correct me if doing wrong

here api

post https://i cant share url http/1.1 content-type: multipart/form-data; boundary=----------------------------8d19a412e59ea7e user-agent: mozilla/5.0 (windows nt 6.1) applewebkit/537.36 (khtml, gecko) chrome/36.0.1985.143 safari/537.36 content-length: 567460 expect: 100-continue connection: keep-alive ------------------------------8d19a412e59ea7e content-disposition: form-data; name="filename"; filename="report.xml" content-type: application/xml 

my request

private string uploadfile(string sourcefile,url)         throws clientprotocolexception, ioexception {     fileinputstream fileinputstream = new fileinputstream(sourcefile);     httpurlconnection conn;     dataoutputstream dos = null;     string lineend = "\r\n";     string twohyphens = "--";     string boundary = "--------------------------8d19a412e59ea7e";     int bytesread, bytesavailable, buffersize;     byte[] buffer;     int maxbuffersize = 1 * 1024 * 1024;     // 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", "");      dos = new dataoutputstream(conn.getoutputstream());      dos.writebytes(boundary + lineend);     dos.writebytes("content-disposition: form-data; name=filename;filename=" + sourcefile+ boundary);      dos.writebytes(lineend);      // create buffer of maximum size     bytesavailable = fileinputstream.available();      buffersize = math.min(bytesavailable, maxbuffersize);     buffer = new byte[buffersize];      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)      string serverresponsemessage = conn.getresponsemessage();     int responsecode = conn.getresponsecode();     bufferedreader br = new bufferedreader(new inputstreamreader(         (conn.getinputstream())));     string inputline = null;     while ((inputline = br.readline()) != null)         system.out.println(inputline);     return serverresponsemessage; } 


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -