springmvc mybatis fileupload实现文件断点续传

本文介绍了一种使用jQuery fileupload插件实现断点续传的方法。通过将文件切分为多个片段上传,并利用Content-Range头告知服务器上传区间,确保了上传过程的可靠性。客户端采用jQuery实现进度显示和取消上传功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://blog.youkuaiyun.com/wang_keng/article/details/50968843

为什么要断点续传:在传输较大文件没传输完成时若出现断网或者服务器异常等情况则文件会上传失败,使用者需要重新开始上传文件,这样会使用户体验十分不好,所以需要有断点续传。断点续传好的方法是将文件分为N个片段进行上传,这样即使后面的片段还未上传完毕之前已上传的片段也会得以保留。

本文使用jQuery fileupload插件进行,它负责将文件分为N个片段进行传输,同时会在http请求头部添加一个请求头:Content-Range;此请求头用于告诉服务器此时传输的是哪个区间的片段。

jsp页面:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  4. <%@include file="WEB-INF/jsp/common/taglibs.jsp"%>  
  5. <!DOCTYPE html>  
  6. <html lang="zh-CN">  
  7. <head>  
  8. <title>用户登录</title>  
  9. <meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1" />  
  10. <meta http-equiv="X-UA-Compatible" content="IE=9" />  
  11. <!-- Bootstrap -->  
  12.   
  13.   
  14. <!--end Bootstrap -->  
  15. <!--[if lt IE 9]>  
  16.       <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>  
  17.       <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>  
  18.     <![endif]-->  
  19. <link href="${ctx}/resources/css/bootstrap.min.css" rel="stylesheet">  
  20. <link href="${ctx}/resources/css/bootstrap-theme.min.css"  
  21.     rel="stylesheet">  
  22. <link rel="stylesheet" href="${ctx}/resources/css/jquery.fileupload.css">    
  23. <link rel="stylesheet" href="${ctx}/resources/css/jquery.fileupload-ui.css">    
  24.   
  25. </head>  
  26. <body>  
  27. <div class="row fileupload-buttonbar" style="padding-left:15px;">    
  28. <div class="thumbnail col-sm-6">    
  29. <!-- <img id="weixin_show" style="height:180px;margin-top:10px;margin-bottom:8px;"  src="__PUBLIC__/images/game/game_1.png" data-holder-rendered="true">   
  30.  --><div class="progress progress-striped active" role="progressbar" aria-valuemin="10" aria-valuemax="100" aria-valuenow="0"><div id="weixin_progress" class="progress-bar progress-bar-success" style="width:0%;"></div></div>    
  31. <div class="caption" align="center">    
  32. <span id="weixin_upload" class="btn btn-primary fileinput-button">    
  33. <span>上传</span>    
  34. <input type="file" id="myFile" name="myFile" multiple>    
  35. </span>    
  36. <a id="weixin_cancle" href="javascript:void(0)" class="btn btn-warning" role="button" onclick="<span style="font-family: Arial, Helvetica, sans-serif;">cancelUpload</span><span style="font-family: Arial, Helvetica, sans-serif;">()" style="display:none">删除</a>  </span>  
  37. <span class="btn btn-warning" id="ase">暂停</span>  
  38. </div>    
  39. </div>    
  40. </div>    
  41.   
  42.   
  43. <script src="${ctx}/resources/js/jquery.min.js"></script>  
  44. <script src="${ctx}/resources/js/bootstrap.min.js"></script>  
  45. <script src="${ctx}/resources/js/jquery.ui.widget.js"></script>    
  46. <script src="${ctx}/resources/js/jquery.fileupload.js"></script>  
  47. <script src="${ctx}/resources/js/jquery.iframe-transport.js"></script>   
  48. <script>  
  49. $(function() {    
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.     var jqXHR;  
  2.     <pre code_snippet_id="1621828" snippet_file_name="blog_20160324_1_4170522" name="code" class="html">    <span style="font-family: Arial, Helvetica, sans-serif;">$("#myFile").fileupload({  </span></pre> url: 'uploadDemo/doUpload', limitConcurrentUploads: 1, sequentialUploads: true, progressInterval: 100, maxChunkSize: 100000, //设置上传片段大小,不设置则为整个文件上传 dataType: "json", add: function (e, data) {<br>  
  3. <span style="white-space:pre"></span>jqXHR = data.submit();<br>  
  4. <span style="white-space:pre"></span>} }).bind('fileuploadprogress', function (e, data) { var progress = parseInt(data.loaded / data.total * 100, 10)-1; $("#weixin_progress").css('width',progress + '%'); $("#weixin_progress").html(progress + '%'); }).bind('fileuploaddone',  
  5.  function (e, data) { $("#weixin_progress").css('width',100 + '%'); $("#weixin_progress").html(100 + '%'); /* $("#weixin_show").attr("src","resource/"+data.result); */ $("#weixin_upload").css({display:"none"}); $("#weixin_cancle").css({display:""}); }).bind('fileuploadpaste',  
  6.  function (e, data) {alert("aaa");}); }); //取消上传 function cancelUpload(){  
  7. <pre></pre>  
  8. <pre code_snippet_id="1621828" snippet_file_name="blog_20160324_1_4170522" name="code" class="html">    <span style="white-space:pre">  jqXHR.abort();</span></pre><pre code_snippet_id="1621828" snippet_file_name="blog_20160324_1_4170522" name="code" class="html">    }  
  9. </script>  
  10. </body>   
  11.   
  12.   
  13. </html></pre><br>  
  14. 服务器代码:<pre code_snippet_id="1621828" snippet_file_name="blog_20160324_2_5741468" name="code" class="java">  @RequestMapping("/doUpload")  
  15.     public @ResponseBody  
  16.     void upload(HttpServletRequest request, PrintWriter writer,  
  17.             HttpServletResponse response) throws Exception {  
  18.   
  19.         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;  
  20.         // 获取传入文件  
  21.         multipartRequest.setCharacterEncoding("utf-8");  
  22.         MultipartFile file = multipartRequest.getFile("myFile");  
  23.   
  24.         this.SaveAs("uploadDemo/" + file.getOriginalFilename(), file, request,  
  25.                 response);  
  26.         // 设置返回值  
  27.         Map<String, String> map = new HashMap<String, String>();  
  28.         map.put("name", file.getOriginalFilename());  
  29.         response.setStatus(200);  
  30.         writer.write(JSON.toJSONString(map));  
  31.     }  
  32.   
  33.     private void SaveAs(String saveFilePath, MultipartFile file,  
  34.             HttpServletRequest request, HttpServletResponse response)  
  35.             throws Exception {  
  36.   
  37.         long lStartPos = 0;  
  38.         int startPosition = 0;  
  39.         int endPosition = 0;  
  40.         int fileLength = 100000;  
  41.         OutputStream fs = null;  
  42.           
  43.         String contentRange = request.getHeader("Content-Range");  
  44. System.out.println(contentRange);  
  45.         if (!new File("uploadDemo").exists()) {  
  46.             new File("uploadDemo").mkdirs();  
  47.         }  
  48.         if (contentRange == null) {  
  49.             FileUtils.writeByteArrayToFile(new File(saveFilePath),  
  50.                     file.getBytes());  
  51.   
  52.         } else {  
  53.             // bytes 10000-19999/1157632     将获取到的数据进行处理截取出开始跟结束位置  
  54.             if (contentRange != null && contentRange.length() > 0) {  
  55.                 contentRange = contentRange.replace("bytes", "").trim();  
  56.                 contentRange = contentRange.substring(0,  
  57.                         contentRange.indexOf("/"));  
  58.                 String[] ranges = contentRange.split("-");  
  59.                 startPosition = Integer.parseInt(ranges[0]);  
  60.                 endPosition = Integer.parseInt(ranges[1]);  
  61.             }  
  62.               
  63.             //判断所上传文件是否已经存在,若存在则返回存在文件的大小  
  64.             if (new File(saveFilePath).exists()) {  
  65.                 fs = new FileOutputStream(saveFilePath, true);  
  66.                 FileInputStream fi = new FileInputStream(saveFilePath);  
  67.                 lStartPos = fi.available();  
  68.                 fi.close();  
  69.             } else {  
  70.                 fs = new FileOutputStream(saveFilePath);  
  71.                 lStartPos = 0;  
  72.             }  
  73.               
  74.             //判断所上传文件片段是否存在,若存在则直接返回  
  75.             if (lStartPos > endPosition) {  
  76.                 fs.close();  
  77.                 return;  
  78.             } else if (lStartPos < startPosition) {  
  79.                 byte[] nbytes = new byte[fileLength];  
  80.                 int nReadSize = 0;  
  81.                 file.getInputStream().skip(startPosition);  
  82.                 nReadSize = file.getInputStream().read(nbytes, 0, fileLength);  
  83.                 if (nReadSize > 0) {  
  84.                     fs.write(nbytes, 0, nReadSize);  
  85.                     nReadSize = file.getInputStream().read(nbytes, 0,  
  86.                             fileLength);  
  87.                 }  
  88.             } else if (lStartPos > startPosition && lStartPos < endPosition) {  
  89.                 byte[] nbytes = new byte[fileLength];  
  90.                 int nReadSize = 0;  
  91.                 file.getInputStream().skip(lStartPos);  
  92.                 int end = (int) (endPosition - lStartPos);  
  93.                 nReadSize = file.getInputStream().read(nbytes, 0, end);  
  94.                 if (nReadSize > 0) {  
  95.                     fs.write(nbytes, 0, nReadSize);  
  96.                     nReadSize = file.getInputStream().read(nbytes, 0, end);  
  97.                 }  
  98.             }  
  99.         }  
  100.         if (fs != null) {  
  101.             fs.flush();  
  102.             fs.close();  
  103.             fs = null;  
  104.         }  
  105.   
  106.     }</pre><br>  
  107. <pre></pre>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值