springMVC实现上传文件功能

来源:互联网 发布:ddns域名注册 编辑:程序博客网 时间:2024/06/09 14:40

1.spring_fileupload.xml配置文件如下:

Xml代码  收藏代码
  1. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  2.     <!--<property name="maxUploadSize" value="10485760"></property>-->  
  3. </bean>  

 

2.FileUploadAction控制器如下:

Java代码  收藏代码
  1.        @RequestMapping(params = "method=up", method = RequestMethod.POST)  
  2. @ResponseBody  
  3. public Map<String, String> uploadSolver(@RequestParam MultipartHttpServletRequest request {  
  4.     Map<String,String> info = new HashMap<String,String>();  
  5.     MultipartFile patch = request.getFile("file");  
  6.     if (!patch.isEmpty()) {  
  7.         try {  
  8.             String fileName = patch.getOriginalFilename();  
  9.   
  10.             /** 
  11.              * 获取文件后缀 
  12.              */  
  13.             System.out.println(fileName);  
  14.             String suffix = fileName.substring(fileName.lastIndexOf("."));  
  15.   
  16.             /** 
  17.              * 判断上传的文件格式是否正确 
  18.              */  
  19.             if ((".zip.rar.gz.tar.bz2.txt".indexOf(suffix.toLowerCase()) != -1)) {  
  20.                 Integer fileSize = (int) patch.getSize() / 1024;  
  21.   
  22.                 /** 
  23.                  * 如果文件小于10M,则上传文件,否则提示用户不能超过10M 
  24.                  */  
  25.                 if (fileSize <= 10240) {  
  26.   
  27.                     String uploadPath = ClassLoaderUtil.getProperties("uploadFile.properties").getProperty("filePath");  
  28.                     System.out.println(uploadPath);  
  29.                     File filePath = new File(request.getSession()  
  30.                             .getServletContext().getRealPath(uploadPath));  
  31.                     System.out.println(filePath.getAbsolutePath());  
  32.                     /** 
  33.                      * 判读存储文件路是否存在,不存在则创建 
  34.                      */  
  35.                     if (! filePath.exists()) {  
  36.                         filePath.mkdirs();  
  37.                         System.out.println("上传文件路径不存在,创建成功!");  
  38.                     }  
  39.                     /** 
  40.                      * 文件开始上传到服务器上 
  41.                      */  
  42.                     patch.transferTo(new File(filePath.getAbsolutePath()+"\\"+fileName));  
  43.                     info.put("success""true");  
  44.                     info.put("msg""上传成功!");  
  45.   
  46.                 } else {  
  47.                     System.out.println("上传的文件太大,文件大小不能超过10M");  
  48.                     info.put("success","false");  
  49.                     info.put("msg""上传的文件太大,文件大小不能超过10M");  
  50.                 }  
  51.             } else {  
  52.                 System.out.println("上传的文件格式不支持");  
  53.                 info.put("success","false");  
  54.                 info.put("msg""上传的文件格式不支持");  
  55.                   
  56.             }  
  57.         } catch (IOException e) {  
  58.             e.printStackTrace();  
  59.             System.out.println("系统异常");  
  60.             info.put("success","false");  
  61.             info.put("msg""系统异常");  
  62.         }  
  63.     }  
  64.     return info;  
  65. }  

 3.前端表单使用Extjs 如下:

Js代码  收藏代码
  1. Ext.onReady(function (){  
  2.       
  3.     var addFileTextField = new Ext.form.TextField({  
  4.         name:'file',  
  5.         allowBlank:false,  
  6.                 //使用HTML中的filetext  
  7.                 inputType:'file'  
  8.               
  9.     });  
  10.       
  11.       
  12.     var addFileFormPanel = new Ext.form.FormPanel({  
  13.          autoDestory:true,  
  14.                  fileUpload:true,  
  15.          frame:true,  
  16.          width:300,  
  17.          autoHeight:true,  
  18.          labelAlign:'right',  
  19.          labelWidth:60,  
  20.          defaultType:'textfield',  
  21.          defaults:{width:200,allowBlank:false},  
  22.            
  23.          items: [addFileTextField]  
  24.     });  
  25.       
  26.       
  27.     var addFileWindow = new Ext.Window({  
  28.             id : "addFileWindow",  
  29.             title : "上传文件",  
  30.             width : 640,  
  31.             height : 200,  
  32.             resizable : false,  
  33.             modal : true,  
  34.             maximizable : false,  
  35.             closeAction : "hide",  
  36.             constrain : true,  
  37.             layout : "vbox",  
  38.             animateTarget:'target',  
  39.             layoutConfig:{  
  40.                 align: "stretch"  
  41.             },  
  42.             items : [addFileFormPanel],  
  43.             buttons:[  
  44.                 {text:'上传',handler:function (){  
  45.                         if(! addFileFormPanel.getForm().isValid()) {  
  46.                             return false;  
  47.                         }  
  48.                         //上传  
  49.                         addFileFormPanel.getForm().submit({  
  50.                             url:'uploadFile.do?method=up',  
  51.                             waitMsg: '正在上传...',  
  52.                             success: function (form,response){  
  53.                                 Ext.Msg.alert('success',response.result.msg);  
  54.                             },  
  55.                             failure: function (form,response){  
  56.                                 Ext.Msg.alert('error',response.result.msg);  
  57.                             }  
  58.                         });  
  59.                     }  
  60.                 }  
  61.             ]  
  62.     });  
  63. });  
 
原创粉丝点击