1.spring_fileupload.xml配置文件如下:
Xml代码
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <!--<property name="maxUploadSize" value="10485760"></property>-->
- </bean>
2.FileUploadAction控制器如下:
Java代码
- @RequestMapping(params = "method=up", method = RequestMethod.POST)
- @ResponseBody
- public Map<String, String> uploadSolver(@RequestParam MultipartHttpServletRequest request {
- Map<String,String> info = new HashMap<String,String>();
- MultipartFile patch = request.getFile("file");
- if (!patch.isEmpty()) {
- try {
- String fileName = patch.getOriginalFilename();
- /**
- * 获取文件后缀
- */
- System.out.println(fileName);
- String suffix = fileName.substring(fileName.lastIndexOf("."));
- /**
- * 判断上传的文件格式是否正确
- */
- if ((".zip.rar.gz.tar.bz2.txt".indexOf(suffix.toLowerCase()) != -1)) {
- Integer fileSize = (int) patch.getSize() / 1024;
- /**
- * 如果文件小于10M,则上传文件,否则提示用户不能超过10M
- */
- if (fileSize <= 10240) {
- String uploadPath = ClassLoaderUtil.getProperties("uploadFile.properties").getProperty("filePath");
- System.out.println(uploadPath);
- File filePath = new File(request.getSession()
- .getServletContext().getRealPath(uploadPath));
- System.out.println(filePath.getAbsolutePath());
- /**
- * 判读存储文件路是否存在,不存在则创建
- */
- if (! filePath.exists()) {
- filePath.mkdirs();
- System.out.println("上传文件路径不存在,创建成功!");
- }
- /**
- * 文件开始上传到服务器上
- */
- patch.transferTo(new File(filePath.getAbsolutePath()+"\\"+fileName));
- info.put("success", "true");
- info.put("msg", "上传成功!");
- } else {
- System.out.println("上传的文件太大,文件大小不能超过10M");
- info.put("success","false");
- info.put("msg", "上传的文件太大,文件大小不能超过10M");
- }
- } else {
- System.out.println("上传的文件格式不支持");
- info.put("success","false");
- info.put("msg", "上传的文件格式不支持");
- }
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("系统异常");
- info.put("success","false");
- info.put("msg", "系统异常");
- }
- }
- return info;
- }
@RequestMapping(params = "method=up", method = RequestMethod.POST) @ResponseBody public MapuploadSolver(@RequestParam MultipartHttpServletRequest request { Map info = new HashMap (); MultipartFile patch = request.getFile("file"); if (!patch.isEmpty()) { try { String fileName = patch.getOriginalFilename(); /** * 获取文件后缀 */ System.out.println(fileName); String suffix = fileName.substring(fileName.lastIndexOf(".")); /** * 判断上传的文件格式是否正确 */ if ((".zip.rar.gz.tar.bz2.txt".indexOf(suffix.toLowerCase()) != -1)) { Integer fileSize = (int) patch.getSize() / 1024; /** * 如果文件小于10M,则上传文件,否则提示用户不能超过10M */ if (fileSize <= 10240) { String uploadPath = ClassLoaderUtil.getProperties("uploadFile.properties").getProperty("filePath"); System.out.println(uploadPath); File filePath = new File(request.getSession() .getServletContext().getRealPath(uploadPath)); System.out.println(filePath.getAbsolutePath()); /** * 判读存储文件路是否存在,不存在则创建 */ if (! filePath.exists()) { filePath.mkdirs(); System.out.println("上传文件路径不存在,创建成功!"); } /** * 文件开始上传到服务器上 */ patch.transferTo(new File(filePath.getAbsolutePath()+"\\"+fileName)); info.put("success", "true"); info.put("msg", "上传成功!"); } else { System.out.println("上传的文件太大,文件大小不能超过10M"); info.put("success","false"); info.put("msg", "上传的文件太大,文件大小不能超过10M"); } } else { System.out.println("上传的文件格式不支持"); info.put("success","false"); info.put("msg", "上传的文件格式不支持"); } } catch (IOException e) { e.printStackTrace(); System.out.println("系统异常"); info.put("success","false"); info.put("msg", "系统异常"); } } return info; }
3.前端表单使用Extjs 如下:
Js代码
- Ext.onReady(function (){
- var addFileTextField = new Ext.form.TextField({
- name:'file',
- allowBlank:false,
- //使用HTML中的filetext
- inputType:'file'
- });
- var addFileFormPanel = new Ext.form.FormPanel({
- autoDestory:true,
- fileUpload:true,
- frame:true,
- width:300,
- autoHeight:true,
- labelAlign:'right',
- labelWidth:60,
- defaultType:'textfield',
- defaults:{width:200,allowBlank:false},
- items: [addFileTextField]
- });
- var addFileWindow = new Ext.Window({
- id : "addFileWindow",
- title : "上传文件",
- width : 640,
- height : 200,
- resizable : false,
- modal : true,
- maximizable : false,
- closeAction : "hide",
- constrain : true,
- layout : "vbox",
- animateTarget:'target',
- layoutConfig:{
- align: "stretch"
- },
- items : [addFileFormPanel],
- buttons:[
- {text:'上传',handler:function (){
- if(! addFileFormPanel.getForm().isValid()) {
- return false;
- }
- //上传
- addFileFormPanel.getForm().submit({
- url:'uploadFile.do?method=up',
- waitMsg: '正在上传...',
- success: function (form,response){
- Ext.Msg.alert('success',response.result.msg);
- },
- failure: function (form,response){
- Ext.Msg.alert('error',response.result.msg);
- }
- });
- }
- }
- ]
- });
- });