httpurlconnection - DataOutputStream to substitute MultipartEntity -
i using apache http upload file (among other parameters) server using code
public httpresponse fileupload(final file file, string auth) throws exception { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(uploadurl); filebody binary = new filebody(file); multipartentity reqentity = new multipartentity(); reqentity.addpart("data_file", binary); reqentity.addpart("auth", new stringbody(auth)); reqentity.addpart("filename", new stringbody(file.getname())); httppost.setentity(reqentity); httpresponse response = httpclient.execute(httppost); return response;} this working need move httpurlconnection since apache methods (and multipartentity) deprecated. instead of doing multiparentity, can create dataoutputstream , use writebytes? how input file write bytes? byte byte or or there easier, cleaner way?
public httpurlconnection uploadfile(final file file, string auth) throws exception { httpurlconnection conn; url url = new url(uploadurl); conn = (httpurlconnection) url.openconnection(); conn.setreadtimeout(read_timeout); conn.setconnecttimeout(connection_timeout); conn.setusecaches(false); conn.setdoinput(true); conn.setdooutput(true); conn.setrequestmethod("post"); conn.connect(); dataoutputstream writer = new dataoutputstream(conn.getoutputstream()); writer.writebytes("data_file"); // send file here writer.flush(); //send auth writer.writebytes("auth"); writer.writebytes(auth); writer.flush(); //send filename writer.writebytes("filename"); writer.writebytes(file.getname()); writer.flush(); return conn; } thanks!
Comments
Post a Comment