This article describes the Java implementation of segmented read files and upload via HTTP method. Share to everyone for your reference. Specifically as follows:
1, the first section of the document, with Randomaccessfile
2, after the paragraph will be separated from the content uploaded to the HTTP
URL url = new URL (actionurl);
HttpURLConnection con = (httpurlconnection) url.openconnection ();
/** allow input, Output, do not use cache/Con.setdoinput (true);
Con.setdooutput (TRUE);
Con.setusecaches (FALSE);
/** set the transmitting Method=post/Con.setrequestmethod ("POST");
/** Setrequestproperty * * * Con.setrequestproperty ("Connection", "keep-alive");
Con.setrequestproperty ("Charset", "UTF-8");
Con.setrequestproperty ("Content-type", "multipart/form-data;boundary=" + boundary);
/** Set DataOutputStream * * DataOutputStream ds = new DataOutputStream (Con.getoutputstream ());
Ds.writebytes (twohyphens + boundary + end);
Ds.writebytes ("Content-disposition:form-data;")
+ "name=\" file1\ "filename=\" "+ newName +" \ "" + end);
Ds.writebytes (end);
/** obtains the document FileInputStream * * * fileinputstream fstream = new FileInputStream (uploadfile);
/** set each write 1024bytes/int buffersize = 1024;
byte[] buffer = new Byte[buffersize];
int length =-1; /** reads data from the file to the buffer */while (length = fstream.read (buffer)!=-1) {/** writes data to DataoutputstreAM Medium */ds.write (buffer, 0, length);
} ds.writebytes (end);
Ds.writebytes (twohyphens + boundary + twohyphens + end);
/** Close Streams * * fstream.close ();
Ds.flush ();
I hope this article will help you with your Java programming.