Java implementation of a segmented read file and upload via HTTP method _java

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.