1 ImportJava.io.BufferedInputStream; 2 ImportJava.io.File; 3 ImportJava.io.FileInputStream; 4 ImportJava.io.IOException; 5 ImportJava.io.InputStream; 6 ImportJava.io.OutputStream; 7 8 ImportJavax.servlet.ServletException; 9 ImportJavax.servlet.http.HttpServlet; Ten ImportJavax.servlet.http.HttpServletRequest; ImportJavax.servlet.http.HttpServletResponse; public class DownLoad extendsHttpServlet {15@Override protected void Service (HttpServletRequest request, httpservletresponse response) throwsServletexception, IOException {String location = "E:\\upload\\1.rar"; 18Downfile (response,request, location); 19 20} private voidDownfile (httpservletresponse response, HttpServletRequest request, String location) {Bufferedinputstream bis = null; Try{The File File = newFile (location); If(File.exists ()) {a long P = 0L; Long tolength = 0L; ContentLength long = 0L; int rangeswitch = 0; 0, full-text download from the beginning, 1, download starting from a byte (bytes=27000-), 2, download from a byte to a byte end (bytes=27000-39000)Filelength; String rangbytes = ""; Filelength =File.length (); /Get file content InputStream ins = newFileInputStream (file); bis = newBufferedinputstream (INS); Panax Notoginseng//Tell the client to allow Accept-ranges 39Response.reset (); Response.setheader ("accept-ranges", "bytes"); //client requests a file block download start byte, String range = Request.getheader ("range"); if (range = null && Range.trim (). Length () > 0 &&! " Null. Equals (range)) {45Response.setstatus (Javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT); Rangbytes = Range.replaceall ("bytes=", "" "); if (Rangbytes.endswith ("-")) {//bytes=270000-48 Rangeswitch = 1; p = Long.parselong (rangbytes.substring (0, Rangbytes.indexof ("-")))); ContentLength = filelength-p; The client requested a byte after 270000 (including a byte with a bytes index of 270000), or else {//bytes=270000-320000 Rangeswitch = 2; Temp1 String = rangbytes.substring (0, Rangbytes.indexof ("-")); Temp2 String = rangbytes.substring (Rangbytes.indexof ("-") + 1, Rangbytes.length ()); p =Long.parselong (TEMP1); Tolength =Long.parselong (TEMP2); ContentLength = tolength-p + 1; The client requested a byte of 270000-320000 between 58}, or else{contentlength =Filelength; 61} 62 63//If Content-length is set, the client will automatically perform multi-threaded downloads. If you do not want to support multi-threading, do not set this parameter. +//content-length: [Total file Size]-[Starting byte of the downloaded file block of the client request] Response.setheader ("Content-length", newLong (contentlength). toString ()); 66 67//Breakpoint Start 68//The format of the response is: content-range:bytes [the total size of the file block's start byte]-[file-The total size of the 1]/[file] if (Rangeswitch = = 1{Contentrange String = new StringBuffer ("bytes"). Append (New Long (P). toString ()). Append ("-") Append (new Long (fileLength-1). toString ()). Append ("/") Append (newLong (filelength). ToString ()). ToString (); Response.setheader ("Content-range", Contentrange); 75Bis.skip (P); Rangeswitch} else if (= = 2{Contentrange String = range.replace ("=", "") + "/" + NEWLong (filelength). toString (); Response.setheader ("Content-range", Contentrange); 79Bis.skip (P); --} else{Bayi String contentrange = new StringBuffer ("bytes"). Append ("0-") Append (fileLength-1). Append ("/") 83. Append (Filelength). toString (); Response.setheader ("Content-range", Contentrange); 85} The String fileName =File.getname (); Response.setcontenttype ("Application/octet-stream"); Response.AddHeader ("Content-disposition", "attachment;filename=" +FileName); OutputStream out =Response.getoutputstream (); A. int n = 0; Readlength Long = 0; 94 int bsize = 1024; byte[] bytes = new byte[Bsize]; if (Rangeswitch = = 2{97//for bytes=27000-39000, write data starting from 27000 98 while (readlength <= contentlength- bsize) {n = bis. Read (bytes), readlength + = n;101 out.write (bytes, 0, N), 102 }103 if (readlength <= contentlength) { 104 n = bis.read (bytes, 0, (int) (ContentLength- readlength)); out.write (bytes, 0, N); 106 } 107} Else {108 while ((n = bis.read (bytes))! =-1 ) {109 Out.write (bytes,0, N); 111 }112 Out.flus H (); 113 out.close (); bis.close (); [+] Else {System.out.println ("location + not Found"); 117 }118} catch (IOException IE) {119} catch (Exception e) { e.printstacktrace (); 121 122 }12 3 }124}
Java Download Breakpoint continuation