The example of this article describes the method of reading files in Java and uploading through HTTP. Share it for everyone for your reference. The specifics are as follows:
1. First of all
2. Upload the contents to HTTP after segmentation
Url url = new url (actionurl); httpurlconnection con = (httpurlconnection) url.openconnection ();/** allows input, output, and do not use cache*/con.SetDoin. put (true); con.setdooutput (true); con. SetUseCaches (FALSE);/** Set the modem = post*/con.setRequestMethod ("Post");/** SetRequestproperty*/Con.SetRequestproperty ("Connection", "" " Keep-alive "); con.setRequestproperty ("Charset", "UTF-8"); Con.SetRequestProperty ("Content-Type", "Multipart/Form-Data; Boundary =" + Boundary); tStream DS = New DataOutputStream ( con.getputstream ()); DS.WriteBytes (Twohyphens + Boundary + End); ds.writeBytes ("Content-DISPOSITION: FORM-DATA;" + "Name =/" file1/"; lename =/" + newme + "/" " + end); ds.writebytes (end);/** FileInputStream*/FileInputStream FSTREAM = New FileInputStream (uploadFile);/** Set at 1024bytes each time t buffersize = 1024; byte [] buffer = new byte [buffersize]; int length = -1;/** read data from file read data to buffer*/while ((length = fStream.read (buffer))! = -1) {/* * Write the data into the DataOutPutStream*/DS.Write (buffer, 0, length);} ds.writebytes (end); ds.writebytes (Twohyphens + Boundary + Twohyphens + End);/* * Close Streams */FSTREAM. close (); ds.flush ();
It is hoped that this article is helpful to everyone's Java program design.