The examples in this article share the code for downloading Java breakpoints for your reference. The specific content is as follows
1. Java code
//Implement file downloadFile(){ File dir = new File(filepath);//Get file path if(!dir.exists()) { System.out.println("File path error"); log.debug("File path error"); return "failed";//Direction whether a file or folder exists} File downloadFile = new File(dir, filename);//Look for file if(!dir.isFile())){ System.out.println("File does not exist"); log.debug("File does not exist"); return "failed";// Determine whether the file or folder exists} try { downloadFileRanges(downloadFile); } catch(ClientAbortException e){ System.out.println("Connection terminated"); log.debug("Connection terminated"); } catch (IOException e) { e.printStackTrace(); } return null; } private void downloadFileRanges(File downloadFile) throws IOException { // File size to be downloaded long fileLength = downloadFile.length(); // File size downloaded long pastLength = 0; // Whether to download the express train, otherwise it is Thunder or other boolean isFlashGet = true; // Used to record the number of end bytes that need to be downloaded (Thunder or other downloads) long lenEnd = 0; // Used to record the data range string required by the client to download String rangeBytes = request.getHeader("Range"); // Used to randomly read and write files RandomAccessFile raf = null; OutputStream os = null; OutputStream outPut = null; byte b[] = new byte[1024]; // If the client download request contains the range if (null != rangeBytes) { // Return code 206 response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); rangeBytes = request.getHeader("Range").replaceAll("bytes=", ""); // Judge Range string mode if (rangeBytes.indexOf('-') == rangeBytes.length() - 1) { // There is no end byte number, fast isFlashGet = true; rangeBytes = rangeBytes.substring(0, rangeBytes.indexOf('-')); pastLength = Long.parseLong(rangeBytes.trim()); } else { // Thunder download isFlashGet = false; String startBytes = rangeBytes.substring(0, rangeBytes.indexOf('-')); String endBytes = rangeBytes.substring( rangeBytes.indexOf('-') + 1, rangeBytes.length()); // Downloaded file segment pastLength = Long.parseLong(startBytes.trim()); // The number of file bytes that need to be downloaded (start from the downloaded file segment) lenEnd = Long.parseLong(endBytes); } } // Notify the client to allow breakpoints to be continuously transmitted, the response format is: Accept-Ranges: bytes response.setHeader("Accept-Ranges", "bytes"); // response.reset(); // If it is the first download, the status defaults to 200, and the response format is: HTTP/1.1 200 ok if (0 != pastLength) { // Content range string String contentRange = ""; // Response format// Content-Range: bytes [start byte of file block]-[Total size of file-1]||[Total size of file] if (isFlashGet) { contentRange = new StringBuffer("bytes") .append(new Long(pastLength).toString()).append("-") .append(new Long(fileLength - 1).toString()) .append("/").append(new Long(fileLength).toString()) .toString(); } else { contentRange = new StringBuffer(rangeBytes).append("/") .append(new Long(fileLength).toString()).toString(); } response.setHeader("Content-Range", contentRange); } String fileName = getDownloadChineseFileName(filename); response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ""); // The format of the response is: response.setContentType("application/octet-stream"); response.addHeader("Content-Length", String.valueOf(fileLength)); try { os = response.getOutputStream(); outPut = new BufferedOutputStream(os); raf = new RandomAccessFile(downloadFile, "r"); // Skip downloaded bytes raf.seek(pastLength); if (isFlashGet) { // Express etc int n = 0; while ((n = raf.read(b, 0, 1024)) != -1) { outPut.write(b, 0, n); } } else { // Thunder etc while (raf.getFilePointer() < lenEnd) { outPut.write(raf.read()); } } outPut.flush(); } catch (IOException e) { /** * When writing data, exceptions such as ClientAbortException* are thrown because the client cancels the download and the server continues to write data to the browser. This is normal. Especially for blood-sucking client software like Thunder. * There is obviously a thread reading bytes=1275856879-1275877358. * If the reading is not completed in a short period of time, Thunder will start the second and third ones again. . . The thread reads the same byte segment until one thread finishes reading. Thunder will KILL * other threads that are downloading the same byte segment, forcibly abort byte reading, causing the server to throw ClientAbortException. * So, we ignore this exception*/ } finally { if(outPut != null) { outPut.close(); } if(raf != null) { raf.close(); } } } private String getDownloadChineseFileName(String paramName) { String downloadChineseFileName = ""; try { downloadChineseFileName = new String(paramName.getBytes("GBK"), "ISO8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return downloadChineseFileName; } public String getFilepath() { return filepath; } public void setFilepath(String filepath) { this.filepath = filepath; } public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } public HttpServletRequest getRequest() { return request; } public HttpServletResponse getResponse() { return response; } 2. Struts section <br />Copy the code as follows:<action name="downloadFile" method="downloadFile">
<result name="failed" type="redirectAction">showDownloadFileNameList</result>
</action>
3. jsp part
The code copy is as follows: <td><a href="downloadFile?filename=${fileMap.key }&&filepath=${fileMap.value }">File download</a></td>