java - Unable to upload file using HttpURLConnection -


i've been using httpurlconnection upload file on execution error "request rejected because no multipart boundary found"

following code snippet

file importfile = new file(args[0]); url = new url("http://localhost:8888/ajax/import?action=csv&session=" + sessionid + "&folder=36"); urlconnection uc = url.openconnection(); connection = (httpurlconnection) url.openconnection(); connection.setrequestmethod("post"); connection.setrequestproperty("cookie", cookiestringbuffer.tostring()); connection.setrequestproperty("content-type", "multipart/form-data"); connection.setdooutput(true); connection.connect();  fileinputstream = new fileinputstream(importfile); outputstream os = connection.getoutputstream(); printwriter pw = new printwriter(os); byte[] buffer = new byte[4096]; int bytes_read; while((bytes_read = is.read(buffer)) != -1) {    //os.write(buffer, 0, bytes_read);    pw.print(buffer); // here "send" our body! } pw.flush(); pw.close(); 

please me out correct problem. urgently need figure out.

thanks lot!

your code copy file output stream wrong, remove line

printwriter pw = new printwriter(os); 

and instead of using pw, write os correct count of bytes read,

os.write(buffer, 0 bytes_read); 

Comments