首页 > 代码库 > springMVC+uploadify3.1 文件上传 demo

springMVC+uploadify3.1 文件上传 demo

uploadify3.1 api 可参考:(点击打开链接)

需要springmvc的jar包

1、upload.jsp(主要代码)

 

[html] view plain copy
 
print?
  1. <script type="text/javascript" charset="UTF-8"  
  2.     src=http://www.mamicode.com/"${pageContext.request.contextPath}/js/jquery1.11.js"></script>  
  3. <script type="text/javascript" charset="UTF-8"  
  4.     src=http://www.mamicode.com/"${pageContext.request.contextPath}/static/llsfw/js/upload/upload.js"></script>  
  5.     <script type="text/javascript" charset="UTF-8"  
  6.     src=http://www.mamicode.com/"${pageContext.request.contextPath}/static/llsfw/common/uploadify/jquery.uploadify.min.js"></script>  
  7.   
  8.   
  9. <div id="uploader">  
  10.         <p>  
  11.             <input type="file" name="file_upload" id="file_upload" />  
  12.         </p>  
  13.         <href=http://www.mamicode.com/"javascript:$(‘#file_upload‘).uploadify(‘upload‘,‘*‘)">上传</a>   
  14.         <href=http://www.mamicode.com/"javascript:$(‘#file_upload‘).uploadify(‘stop‘)">取消上传</a>  
  15.         <div id="uploader_queue"></div>  
  16.         <div id="uploader_msg"></div>  
  17.         <div id="uploader_view"></div>  
  18.     </div>  
技术分享

2、upload.js

 

 

[javascript] view plain copy
 
print?
  1. $(function() {  
  2.      $("#file_upload").uploadify({  
  3.            ‘auto‘ : false,  
  4.            ‘method‘ : "get",  
  5.            ‘formData‘ : {‘folder‘ : ‘file‘},  
  6.               ‘height‘ : 30,  
  7.               ‘swf‘ : basePath+‘static/llsfw/common/uploadify/uploadify.swf‘, // flash  
  8.               ‘uploader‘ : basePath+‘uploadController/upload‘, // 数据处理url  
  9.               ‘width‘ : 120,  
  10.               ‘fileTypeDesc‘ : ‘只能是xls...‘,  
  11.               ‘fileTypeExts‘ : ‘*.xls‘,  
  12.               ‘fileSizeLimit‘ : ‘10500KB‘,  
  13.               ‘buttonText‘ : ‘选择文件‘,  
  14.               ‘uploadLimit‘ : 5,  
  15.               ‘successTimeout‘ : 5,  
  16.               ‘requeueErrors‘ : false,  
  17.               ‘removeTimeout‘ : 10,  
  18.               ‘removeCompleted‘ : false,  
  19.               ‘queueSizeLimit‘ :10,  
  20.               ‘queueID‘  : ‘uploader_queue‘,  
  21.               ‘progressData‘ : ‘speed‘,  
  22.               ‘onInit‘ : function (){},  
  23.            // 单个文件上传成功时的处理函数  
  24.            ‘onUploadSuccess‘ : function(file, data, response){  
  25.          $("#uploader_view").append(‘<div height="60" >‘+data+‘</div>‘);  
  26.         },  
  27.               ‘onQueueComplete‘ : function(queueData) {  
  28.          $(‘#uploader_msg‘).html(queueData.uploadsSuccessful + ‘ 个文件上传成功。‘);  
  29.         }        
  30.           });  
  31.       
  32. });  
技术分享

 

3、UploadController.java
[java] view plain copy
 
print?
  1. package com.llsfw.core.controller.upload;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.Iterator;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8. import org.springframework.stereotype.Controller;  
  9. import org.springframework.web.bind.annotation.RequestMapping;  
  10. import org.springframework.web.multipart.MultipartFile;  
  11. import org.springframework.web.multipart.MultipartHttpServletRequest;  
  12. import org.springframework.web.multipart.commons.CommonsMultipartResolver;  
  13.   
  14. import com.llsfw.core.controller.base.BaseController;  
  15.   
  16. @Controller  
  17. @RequestMapping("uploadController")  
  18. public class UploadController extends BaseController {  
  19.     @RequestMapping("init")  
  20.     public String init() {  
  21.         return "llsfw/upload/upload";  
  22.     }  
  23.   
  24.     @RequestMapping("upload")  
  25.     public void upload(HttpServletRequest request, HttpServletResponse response) {  
  26.         try {  
  27.             String savePath = "C://uploads/";  
  28.             File dirPath = new File(savePath);    
  29.             if (!dirPath.exists()) {    
  30.                 dirPath.mkdirs();    
  31.             }   
  32.             // 解析器解析request的上下文  
  33.             CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(  
  34.                     request.getSession().getServletContext());  
  35.             // 先判断request中是否包涵multipart类型的数据,  
  36.             if (multipartResolver.isMultipart(request)) {  
  37.                 // 再将request中的数据转化成multipart类型的数据  
  38.                 MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;  
  39.                 @SuppressWarnings("rawtypes")  
  40.                 Iterator iter = multiRequest.getFileNames();  
  41.   
  42.                 while (iter.hasNext()) {  
  43.                     MultipartFile file = multiRequest.getFile((String) iter.next());  
  44.                     if (file != null) {  
  45.                         String fileName = file.getOriginalFilename();  
  46.                         System.out.println(fileName);  
  47.                         File localFile = new File(savePath + fileName);  
  48.                         // 写文件到本地  
  49.                         file.transferTo(localFile);  
  50.                         response.getWriter().print(fileName+"        上传成功!");  
  51.                     }  
  52.                 }  
  53.             }  
  54.               
  55.         } catch (IOException e) {  
  56.             // TODO Auto-generated catch block  
  57.             e.printStackTrace();  
  58.         }  
  59.     }  
  60. }  
技术分享




springMVC+uploadify3.1 文件上传 demo