本文實例為大家分享了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部分<br />複製代碼代碼如下:<action name="downloadFile" method="downloadFile">
<result name="failed" type="redirectAction">showDownloadFileNameList</result>
</action>
3. jsp部分
複製代碼代碼如下:<td><a href="downloadFile?filename=${fileMap.key }&&filepath=${fileMap.value }">文件下載</a></td>