Java/servlet breakpoint download

Source: Internet
Author: User

// Path of the downloaded File: String path = "D: \ TDDownload \ system \", fileName = "cn_windows_8_1_pro_vl_x86_dvd_291_1_iso"; file File = new File (path + fileName ); inputStream in = new FileInputStream (file); OutputStream OS = resp. getOutputStream (); // clear the data in the response cache. Only the status code and Header resp are retained. reset (); byte [] buffer = new byte [128]; long downloaded = 0; String range = req. getHeader ("range"); if (range! = Null) {downloaded = Long. parseLong (range. split ("-") [0]. split ("=") [1]); // If the 206 status is set, the resp can be downloaded from a breakpoint. setStatus (206); // sets the download range resp. setHeader ("Content-Range", "bytes" + downloaded + "-" + file. length () + "/" + file. length ();} resp. setContentType ("application/octet-stream"); // file name resp. addHeader ("Content-Disposition", "attachment; filename =" + new String (fileName. getBytes (); // file size resp. addHeader ("Content-Lengt H ", file. length () + ""); // The accept range is bytes resp. setHeader ("Accept-Ranges", "bytes"); int len = 0; while (len = in. read (buffer ))! =-1) {OS. write (buffer, 0, len);} in. close (); OS. flush (); OS. close ();

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.