现在的位置: 首页 > 综合 > 正文

uploadify 3.2.1+spring mvc上传文件

2016年12月03日 ⁄ 综合 ⁄ 共 2204字 ⁄ 字号 评论关闭

http://wdwheyr.iteye.com/blog/1992158

之前做过uploadify 2.x的文件上传,做这个项目的时候发现官网现在版本是3.2.1于是就做了一个demo。 
前台代码如下(注意引入jquery):

Java代码  收藏代码
  1. <!DOCTYPE HTML >  
  2. <html>  
  3.   <head>  
  4.     <title>文件上传</title>  
  5.      <%@include file="comm/comm.jsp" %>  
  6.      <link rel="stylesheet" type="text/css" href="css/uploadify.css">  
  7.      <script type="text/javascript" src="js/jquery.uploadify.min.js"></script>  
  8.   <script >  
  9.       $(document).ready(function() {  
  10.             $("#file_upload").uploadify({  
  11.                     'buttonText' : '请选择',  //按钮显示的文字
  12.                     'height' : 30,  //按钮的高
  13.                     'swf' : '/auth/js/uploadify.swf',  
  14.                     'uploader' : '/auth/uploadFile.html',  //post请求的地址
  15.                     'width' : 120,  //按钮的长
  16.                     'auto':false,  
  17.                     'fileObjName'   : 'file',  //file对象的名字,相当于id="file"
  18.                      'fileTypeExts' :    '*.jpg; *.png; *.JPG ; *.PNG', //限定上传文件的类型
  19.                     'onUploadSuccess' : function(file, data, response) {  
  20.                         alert( file.name + ' 上传成功! ');  
  21.                     }  
  22.                 });  
  23.         });  
  24.      </script>  
  25.   </head>  
  26.   <body>  
  27.     <input type="file" name="fileName" id="file_upload" />  
  28.     <a href="javascript:$('#file_upload').uploadify('upload', '*')">上传文件</a> | <a href="javascript:$('#file_upload').uploadify('stop')">停止上传!</a>  
  29.   </body>  
  30. </html>  


后台代码如下: 

Java代码  收藏代码
  1. @ResponseBody  //返回的是字符串,不能是view,所以要加这个标签
  2. @RequestMapping(value="/uploadFile",method=RequestMethod.POST)  
  3.     public void uploadFile(HttpServletResponse response,HttpServletRequest request,@RequestParam(value="file", required=false) MultipartFile file) throws IOException{  
  4.         byte[] bytes = file.getBytes();  
  5.         System.out.println(file.getOriginalFilename());  
  6.         String uploadDir = request.getRealPath("/")+"upload";  
  7.         File dirPath = new File(uploadDir);  
  8.         if (!dirPath.exists()) {  
  9.             dirPath.mkdirs();  
  10.         }  
  11.         String sep = System.getProperty("file.separator");  
  12.         File uploadedFile = new File(uploadDir + sep  
  13.                 + file.getOriginalFilename());  
  14.         FileCopyUtils.copy(bytes, uploadedFile);  
  15.         msg = "true";  
  16.         response.getWriter().write(msg);  
  17.     }  

抱歉!评论已关闭.