本文執行個體為大家分享了java斷點續傳下載的代碼,供大家參考,具體內容如下
1. Java代碼
//實現檔案下載功能 public String downloadFile(){ File dir = new File(filepath);//擷取檔案路勁 if(!dir.exists()) { System.out.println("檔案路徑錯誤"); log.debug("檔案路徑錯誤"); return "failed";// 判斷檔案或檔案夾是否存在 } File downloadFile = new File(dir, filename);//在指定目錄下尋找檔案 if(!downloadFile.isFile()){ System.out.println("檔案不存在"); log.debug("檔案不存在"); return "failed";// 判斷檔案或檔案夾是否存在 } try { downloadFileRanges(downloadFile); } catch(ClientAbortException e){ System.out.println("串連被終止"); log.debug("串連被終止"); } catch (IOException e) { e.printStackTrace(); } return null; } private void downloadFileRanges(File downloadFile) throws IOException { // 要下載的檔案大小 long fileLength = downloadFile.length(); // 已下載的檔案大小 long pastLength = 0; // 是否快車下載,否則為迅雷或其他 boolean isFlashGet = true; // 用於記錄需要下載的結束位元組數(迅雷或其他下載) long lenEnd = 0; // 用於記錄用戶端要求下載的資料範圍字串 String rangeBytes = request.getHeader("Range"); //用於隨機讀取寫入檔案 RandomAccessFile raf = null; OutputStream os = null; OutputStream outPut = null; byte b[] = new byte[1024]; // 如果用戶端下載請求中包含了範圍 if (null != rangeBytes) { // 返回碼 206 response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); rangeBytes = request.getHeader("Range").replaceAll("bytes=", ""); // 判斷 Range 字串模式 if (rangeBytes.indexOf('-') == rangeBytes.length() - 1) { // 無結束位元組數,為快車 isFlashGet = true; rangeBytes = rangeBytes.substring(0, rangeBytes.indexOf('-')); pastLength = Long.parseLong(rangeBytes.trim()); } else { // 迅雷下載 isFlashGet = false; String startBytes = rangeBytes.substring(0, rangeBytes.indexOf('-')); String endBytes = rangeBytes.substring( rangeBytes.indexOf('-') + 1, rangeBytes.length()); // 已下載檔案段 pastLength = Long.parseLong(startBytes.trim()); // 還需下載的檔案位元組數(從已下載檔案段開始) lenEnd = Long.parseLong(endBytes); } } // 通知用戶端允許斷點續傳,響應格式為:Accept-Ranges: bytes response.setHeader("Accept-Ranges", "bytes"); // response.reset(); // 如果為第一次下載,則狀態預設為 200,響應格式為: HTTP/1.1 200 ok if (0 != pastLength) { // 內容約制字串 String contentRange = ""; // 響應格式 // Content-Range: bytes [檔案塊的開始位元組]-[檔案的總大小 - 1]||[檔案的總大小] 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 + ""); // 響應的格式是: 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"); // 跳過已下載位元組 raf.seek(pastLength); if (isFlashGet) { // 快車等 int n = 0; while ((n = raf.read(b, 0, 1024)) != -1) { outPut.write(b, 0, n); } } else { // 迅雷等 while (raf.getFilePointer() < lenEnd) { outPut.write(raf.read()); } } outPut.flush(); } catch (IOException e) { /** * 在寫資料的時候 對於 ClientAbortException 之類的異常 * 是因為用戶端取消了下載,而伺服器端繼續向瀏覽器寫入資料時, 拋出這個異常,這個是正常的。 尤其是對於迅雷這種吸血的用戶端軟體。 * 明明已經有一個線程在讀取 bytes=1275856879-1275877358, * 如果短時間內沒有讀取完畢,迅雷會再啟第二個、第三個。。。線程來讀取相同的位元組段, 直到有一個線程讀取完畢,迅雷會 KILL * 掉其他正在下載同一位元組段的線程, 強行中止位元組讀出,造成伺服器拋 ClientAbortException。 * 所以,我們忽略這種異常 */ } 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部分
複製代碼 代碼如下:
<action name="downloadFile" class="downloadFileAction" method="downloadFile">
<result name="failed" type="redirectAction">showDownloadFileNameList</result>
</action>
3. jsp部分
複製代碼 代碼如下:
<td><a href="downloadFile?filename=${fileMap.key }&&filepath=${fileMap.value }">檔案下載</a></td>