webuploader+springmvc实现多文件上传(html+js+css原创,后台代码借鉴)

———————-css—————————-

 
  1. /*添加图片按钮*/

  2. .add_resume_item {

  3. cursor: pointer;

  4. }

  5. /*遮罩层*/

  6. .zpzs_gray {

  7. position: fixed;

  8. left: 0;

  9. top: 0;

  10. width: 100%;

  11. height: 100%;

  12. background-color: gray;

  13. opacity: 0.6;

  14. filter: alpha(opacity=60);

  15. display: none;

  16. z-index: 100;

  17. }

  18. /*弹出框*/

  19. #uploader {

  20. display: none;

  21. height: 414px;

  22. width: 830px;

  23. position: fixed;

  24. margin-top: -207px;

  25. margin-left: -415px;

  26. top: 50%;

  27. left: 50%;

  28. background-color: #fff;

  29. border: 1px solid #ddd;

  30. z-index: 101;

  31. }

  32. /*弹出框头部*/

  33. #uploader .add_img_head {

  34. height: 44px;

  35. font-size: 12px;

  36. background-color: #ff8400;

  37. position: relative;

  38. color: #fff;

  39. }

  40. #uploader .add_img_head span {

  41. background: url("../images/imgHead_grzp.png") 0 0 no-repeat;

  42. position: absolute;

  43. display: block;

  44. top: 12px;

  45. left: 5px;

  46. width: 180px;

  47. height: 20px;

  48. }

  49. #uploader .add_img_head b {

  50. background: url("../images/closeImg.png") 0 0 no-repeat;

  51. position: absolute;

  52. display: block;

  53. top: 0;

  54. right: 0;

  55. width: 32px;

  56. height: 21px;

  57. cursor: pointer;

  58. }

  59. /*选择图片按钮*/

  60. #uploader .add_img {

  61. height: 150px;

  62. width: 300px;

  63. position: absolute;

  64. margin-top: -75px;

  65. margin-left: -150px;

  66. top: 50%;

  67. left: 50%;

  68. }

  69. #uploader .add_img p {

  70. text-align: center;

  71. color: #999;

  72. font-size: 14px;

  73. }

  74. #uploader .add_img .uploadBtn {

  75. display: none;

  76. position: absolute;

  77. right: 10px;

  78. width: 118px;

  79. height: 38px;

  80. line-height: 38px;

  81. top: 8px;

  82. cursor: pointer;

  83. text-align: center;

  84. color: #fff;

  85. background: url("../images/uploaderbtn.png") 0 0 no-repeat;

  86. }

  87. #uploader .add_img #filePicker {

  88. height: 38px;

  89. width: 128px;

  90. text-align: center;

  91. color: #fff;

  92. line-height: 38px;

  93. margin-left: 86px;

  94. position: relative;

  95. margin-bottom: 20px;

  96. background: url("../images/chooseImg_grzp.png") 0 0 no-repeat;

  97. }

  98. #uploader .add_img #filePicker div {

  99. width: 100% !important;

  100. height: 100% !important;

  101. position: absolute;

  102. top: 0;

  103. left: 0;

  104. opacity: 0;

  105. filter: alpha(opacity=0);

  106. }

  107. #uploader .add_img #filePicker div.webuploader-pick {

  108. opacity: 1;

  109. filter: alpha(opacity=100);

  110. color: #fff;

  111. }

  112. #uploader .add_img #filePicker input,

  113. #uploader .add_img #filePicker label{

  114. width: 100% !important;

  115. height: 100% !important;

  116. position: absolute;

  117. top: 0;

  118. left: 0;

  119. }

  120. #uploader #fileList {

  121. padding-left: 20px;

  122. }

  123. /*单个缩略图容器*/

  124. #uploader .file-item {

  125. float: left;

  126. width: 118px;

  127. overflow: hidden;

  128. border: 1px solid #969696;

  129. height: 128px;

  130. line-height: 128px;

  131. text-align: center;

  132. margin-left: 10px;

  133. margin-top: 10px;

  134. position: relative;

  135. }

  136. #uploader .file-item img {

  137. width: 100%;

  138. vertical-align: middle;

  139. }

  140. #uploader .file-item .info {

  141. position: absolute;

  142. width: 100%;

  143. height: 28px;

  144. bottom: 0;

  145. left: 0;

  146. background-color: #ddd;

  147. cursor: pointer;

  148. line-height: 28px;

  149. }

  150. #uploader .file-item p.progress {

  151. position: absolute;

  152. width: 100%;

  153. bottom: 0;

  154. left: 0;

  155. height: 18px;

  156. overflow: hidden;

  157. z-index: 50;

  158. margin: 0;

  159. border-radius: 0;

  160. background: #e8e8e8;

  161. -webkit-box-shadow: 0 0 0;

  162. }

  163. #uploader .file-item p.progress span {

  164. display: block;

  165. overflow: hidden;

  166. width: 0;

  167. height: 100%;

  168. background: #f4b887;

  169. -webit-transition: width 200ms linear;

  170. -moz-transition: width 200ms linear;

  171. -o-transition: width 200ms linear;

  172. -ms-transition: width 200ms linear;

  173. transition: width 200ms linear;

  174. -webkit-animation: progressmove 2s linear infinite;

  175. -moz-animation: progressmove 2s linear infinite;

  176. -o-animation: progressmove 2s linear infinite;

  177. -ms-animation: progressmove 2s linear infinite;

  178. animation: progressmove 2s linear infinite;

  179. -webkit-transform: translateZ(0);

  180. }

———————-js—————————-
1.自行下载webuploader.js

 
  1. jQuery(function () {

  2. var $ = jQuery,

  3. //弹出上传框

  4. $wrap = $('#uploader'),

  5. //图片缩略图容器

  6. $list = $('#fileList'),

  7. //开始上传按钮

  8. $upload = $wrap.find('.uploadBtn'),

  9. //缩略图压缩程度

  10. ratio = window.devicePixelRatio || 1,

  11. //缩略图大小

  12. thumbnailWidth = 100 * ratio,

  13. thumbnailHeight = 100 * ratio,

  14. //Web Uploader实例

  15. uploader;

  16. //初始化Web Uploader

  17. uploader = WebUploader.create({

  18. //自动上传。

  19. auto: false,

  20. //swf文件路径

  21. swf: basePath + 'picture/Uploader.swf',

  22. //文件接收服务端。

  23. server: basePath + 'uploadPicture',

  24. //选择文件的按钮。

  25. pick: '#filePicker',

  26. //单次上传最多图片数

  27. fileNumLimit: 12,

  28. //单个文件最大为2m

  29. fileSingleSizeLimit: 2*1024*1024,

  30. //允许选择的图片格式

  31. accept: {

  32. title: 'Images',

  33. extensions: 'gif,jpg,jpeg,bmp,png',

  34. mimeTypes: 'image/*'

  35. }

  36. });

  37.  
  38. // 当有文件添加进来的时候

  39. uploader.on('fileQueued', function (file) {

  40. //图片数目限制为12张

  41. if ($('.file-item').length < 12) {

  42. //创建新添加图片和删除条

  43. var $li = $(

  44. '<div id="' + file.id + '" class="file-item">' +

  45. '<img>' +

  46. '<div class="info">删除</div>' +

  47. '</div>'

  48. ),

  49. $info = $li.find('.info'),

  50. $img = $li.find('img');

  51. //将新添加图片放入缩略图容器

  52. $list.append($li);

  53. //为图片删除条添加单击删除事件

  54. $info.on('click', function () {

  55. //将图片移除上传序列

  56. uploader.removeFile(file, true);

  57. //将图片从缩略图容器删除

  58. var $li = $('#' + file.id);

  59. $li.off().remove();

  60. $('#filePicker').children().css('display','');

  61. if ($('#filePicker').attr('class') === 'qyfc_upload webuploader-container') {

  62. $('#filePicker').css('background', 'url("images/chooseImg_qyfc.png") 0 0 no-repeat');

  63. } else {

  64. $('#filePicker').css('background', 'url("images/chooseImg_grzp.png") 0 0 no-repeat');

  65. }

  66. });

  67. // 创建缩略图

  68. uploader.makeThumb(file, function (error, src) {

  69. if (error) {

  70. $img.replaceWith('<span>不能预览</span>');

  71. return;

  72. }

  73. $img.attr('src', src);

  74. }, thumbnailWidth, thumbnailHeight);

  75. //添加图片完成后将添加按钮和上传按钮固定在弹出框底部

  76. $('.add_img').css({

  77. 'left': '0',

  78. 'top': '350px',

  79. 'width': '100%',

  80. 'margin-top': '0',

  81. 'margin-left': '0',

  82. 'height': '60px',

  83. 'padding-top': '8px'

  84. });

  85. //为弹出框内元素更改样式和添加事件

  86. $('.uploadBtn').css({'display': 'block'});

  87. $('#filePicker').css({'margin-left': '540px'});

  88. $('.add_img p').css({'display': 'none'});

  89. if ($('.file-item').length >= 12) {

  90. $('#filePicker').children().css('display', 'none');

  91. if ($('#filePicker').attr('class') === 'qyfc_upload webuploader-container') {

  92. $('#filePicker').css('background', 'url("images/chooseImg_qyfcdis.png") 0 0 no-repeat');

  93. } else {

  94. $('#filePicker').css('background', 'url("images/chooseImg_grzpdis.png") 0 0 no-repeat');

  95. }

  96. }

  97. }

  98. });

  99.  
  100. // 文件上传过程中创建进度条实时显示。

  101. uploader.on('uploadProgress', function (file, percentage) {

  102. var $li = $('#' + file.id),

  103. $percent = $li.find('.progress span'),

  104. $info = $li.find('.info');

  105. // 避免重复创建

  106. if (!$percent.length) {

  107. $percent = $('<p class="progress"><span></span></p>')

  108. .appendTo($li).find('span');

  109. }

  110. $info.css({display: 'none'});

  111. $percent.css('width', percentage * 100 + '%');

  112. });

  113.  
  114. // 文件上传成功,给info添加文字,标记上传成功。

  115. uploader.on('uploadSuccess', function (file) {

  116. var $li = $('#' + file.id), $info = $li.find('.info');

  117. $li.off();

  118. $info.off().text('上传成功!')

  119. .css({color: 'green', display: 'block'});

  120. });

  121.  
  122. // 文件上传失败,给info添加文字,标记上传失败。

  123. uploader.on('uploadError', function (file) {

  124. var $li = $('#' + file.id),

  125. $info = $li.find('.info');

  126. $info.off().text('上传失败!')

  127. .css({color: 'red', display: 'block'});

  128. });

  129.  
  130. // 上传成功或失败后删除进度条。

  131. uploader.on('uploadComplete', function (file) {

  132. $('#' + file.id).find('.progress').remove();

  133. });

  134.  
  135. //单击开始上传按钮开始上传

  136. $upload.on('click', function () {

  137. if ($('#fileList').children().length) {

  138. $('.uploadBtn').css('background', 'url("images/uploaderbtndis.png") 0 0 no-repeat');

  139. if ($('#filePicker').attr('class') === 'qyfc_upload webuploader-container') {

  140. $('#filePicker').css('background', 'url("images/chooseImg_qyfcdis.png") 0 0 no-repeat');

  141. } else {

  142. $('#filePicker').css('background', 'url("images/chooseImg_grzpdis.png") 0 0 no-repeat');

  143. }

  144. $('#filePicker').children().css('display', 'none');

  145. timer = setInterval(function () {

  146. if ($upload.html() === '重新上传' || $upload.html() === '开始上传' || $upload.html() === '上传中...') {

  147. $upload.html('上传中')

  148. } else {

  149. $upload.html($upload.html() + '.')

  150. }

  151. }, 500);

  152. $('.info').html('等待上传..').off();

  153. if ($upload.html() === '重新上传') {

  154. uploader.retry();

  155. } else {

  156. uploader.upload();

  157. }

  158. }

  159. });

  160.  
  161. //全部上传完成时触发关闭弹出层

  162. uploader.on('uploadFinished', function () {

  163. $('.uploadBtn').css('background', 'url("images/uploaderbtn.png") 0 0 no-repeat');

  164. clearInterval(timer);

  165. closeBtn();

  166. });

  167.  
  168. //单击页面上的上传图片选项弹出上传框

  169. $('.add_resume_item').click(function () {

  170. $('.zpzs_gray,#uploader').css('display', 'block');

  171. });

  172.  
  173. //单击上传框上叉号按钮添加关闭上传框

  174. $('.closeBtn').click(closeBtn);

  175.  
  176. //关闭弹出窗

  177. function closeBtn() {

  178. //获取上传出错和未上传的图片

  179. var allPic = $('#fileList').children().length,

  180. inited = uploader.getFiles('inited').length,

  181. error = uploader.getFiles('error').length,

  182. queued = uploader.getFiles('queued').length;

  183. //判断是否可以关闭窗口

  184. if (error) {

  185. $upload.html('重新上传');

  186. if (window.confirm('您已上传成功' + (allPic - error) + '张,上传失败' + error + '张,可能由于网络原因上传失败,是否确认关闭窗口!')) {

  187. closeuploader();

  188. }

  189. } else if (inited) {

  190. $upload.html('开始上传');

  191. if ($('#filePicker').attr('class') === 'qyfc_upload webuploader-container') {

  192. var text = '企业风采';

  193. } else {

  194. var text = '个人作品';

  195. }

  196. if (window.confirm('您还有'+text+'尚未上传!\r\r是否确认取消上传?')) {

  197. closeuploader();

  198. }

  199. } else if (queued) {

  200. if (window.confirm('您还有' + text + '等待上传!\r\r是否确认关闭窗口!')) {

  201. closeuploader();

  202. }

  203. } else {

  204. if (allPic) {

  205. window.location.href = window.location.href;

  206. }

  207. closeuploader();

  208. }

  209. //关闭上传框窗口后恢复上传框初始状态

  210. function closeuploader() {

  211. //关闭上传框

  212. $('.zpzs_gray,#uploader').css('display', 'none');

  213. //移除所有缩略图并将图片移除上传序列

  214. for (var i = 0; i < uploader.getFiles().length; i++) {

  215. //将图片从上传序列移除

  216. uploader.removeFile(uploader.getFiles()[i], true);

  217. delete uploader.getFiles()[i];

  218. //将图片从缩略图容器移除

  219. var $li = $('#' + uploader.getFiles()[i].id);

  220. $li.off().remove();

  221. }

  222. //恢复上传框内元素样式

  223. $('#filePicker,.uploadBtn,.add_img p,.add_img').removeAttr("style");

  224. if ($('#filePicker').attr('class') === 'qyfc_upload webuploader-container') {

  225. $('#filePicker').css('background', 'url("images/chooseImg_qyfc.png") 0 0 no-repeat');

  226. } else {

  227. $('#filePicker').css('background', 'url("images/chooseImg_grzp.png") 0 0 no-repeat');

  228. }

  229. $('#filePicker').children().removeAttr("style");

  230. $upload.html('开始上传');

  231. }

  232. }

  233. });

  234. /**

  235. * 显示文件上传弹层

  236. * @return

  237. */

  238. function showUploadFrame(){

  239. $('.zpzs_gray,#uploader').css('display', 'block');

  240. }

  • 20

———————html———————————————-

 
  1. <!--弹出遮罩层-->

  2. <div class="zpzs_gray"></div>

  3. <!--上传框开始-->

  4. <div id="uploader">

  5. <!--上传框头部-->

  6. <div class="add_img_head">

  7. <span></span>

  8. <b class="closeBtn"></b>

  9. </div>

  10. <!--缩略图容器-->

  11. <div id="fileList"></div>

  12. <!--选择图片按钮-->

  13. <div class="add_img">

  14. <div id="filePicker"></div>

  15. <p>按住Ctrl可多选照片</p>

  16.  
  17. <p>单次上传最多12张(单张最大2M)</p>

  18.  
  19. <div class="uploadBtn">开始上传</div>

  20. </div>

  21. </div>

  22. <!--上传框结束-->

  23. <script src="<%=basePath %>js/jobSoo/startSet1.js"></script>

  • 1
  • 2

—————————-后台代码———————————————————-

Controller层

 
  1. @RequestMapping("uploadPicture")

  2. @ResponseBody

  3. public void uploads(@RequestParam("file")MultipartFile[] files, String destDir,

  4. HttpServletRequest request,HttpServletResponse response) {

  5. try {

  6. userUploadPictureService.uploads(files, destDir, request,response);

  7. } catch (Exception e) {

  8. e.printStackTrace();

  9. }

  10. }

  • 1

Service层

 
  1. public void uploads(MultipartFile[] files, String destDir,

  2. HttpServletRequest request,HttpServletResponse response) throws Exception {

  3. User user = (User)request.getSession().getAttribute("user");

  4. String path = request.getContextPath();

  5. String basePath = request.getScheme() + "://" + request.getServerName() + ":" +

  6. request.getServerPort() + path;

  7. // 获取文件上传的真实路径

  8. String uploadPath = request.getSession().getServletContext().getRealPath("/");

  9. List<String> picPaths = new ArrayList<String>();

  10. try {

  11. fileNames = new String[files.length];

  12. int index = 0;

  13. //上传文件过程

  14. for (MultipartFile file : files) {

  15. String suffix=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);

  16. int length = getAllowSuffix().indexOf(suffix);

  17. if (length == -1) {

  18. throw new Exception("请上传允许格式的文件");

  19. }

  20. destDir = "staticResource/user/picture/" + user.getId();

  21. File destFile = new File(uploadPath + destDir);

  22. if (!destFile.exists()) {

  23. destFile.mkdirs();

  24. }

  25. String fileNameNew = getFileNameNew() + "." + suffix;//

  26. File f = new File(destFile.getAbsoluteFile() + File.separator + fileNameNew);

  27. //如果当前文件已经存在了,就跳过。

  28. if(f.exists()){

  29. continue;

  30. }

  31. file.transferTo(f);

  32. f.createNewFile();

  33. fileNames[index++] = basePath + destDir + fileNameNew;

  34. }

  35. //个人作品展示

  36. if(user.getUserType() == 0){

  37. Resume resume = resumeDao.findResumeByUserId(user.getId());

  38. String resumeRank = resume.getResumeRank();

  39. //若不存在,添加上“作品展示”,并更新

  40. if(resumeRank.indexOf(":zpzs") == -1){

  41. resumeRank = resumeRank+":zpzs";

  42. resume.setResumeRank(resumeRank);

  43. resumeDao.updateResume(resume);

  44. }

  45. request.getSession().setAttribute("user", user);

  46. }

  47. } catch (Exception e) {

  48. throw e;

  49. }

  50. }

  51.  
  52. /**

  53. * 为文件重新命名,命名规则为当前系统时间毫秒数

  54. * @return string

  55. */

  56. private String getFileNameNew() {

  57. SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");

  58. return fmt.format(new Date());

  59. }

猜你喜欢

转载自blog.csdn.net/eIoBhMojo/article/details/87878866