`
endual
  • 浏览: 3513691 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

[应用架构] SpringMVC文件上传例子

    博客分类:
  • java
 
阅读更多
 

[应用架构] SpringMVC文件上传例子

分类: J2EE企业级开发 782人阅读 评论(0) 收藏 举报
1.上传用到的一些表单控件,如下所示:

 

 

  1. <div class="std_photo">                                 
  2.     请上传图片:<img id="pictureImage1" name="pictureImage1"  src="${userDto.pictureImage}"  height="200" width="100"  />  
  3.                 <input type="hidden" id="pictureImage"  name="pictureImage" value="">   
  4.                 <input id="file" type="file" name="file" />   
  5.                 <input type="button" value="保存"   class="btn" />  
  6.                 
  7. </div>  

 

其中 类型type为hidden的控件为一个隐藏域,它专门用来用userDto里面的pictureImage交互的。而img图片控件同userDto.pictureImage绑定起来。 通过点击保存响应一个ajax事件.



2.ajax响应事件,如下所示:

 

[javascript] view plaincopyprint?
  1. function ajaxFileUpload() {  
  2.               
  3.             $.ajaxFileUpload({  
  4.                     url : 'FileUploadForm.do?method=uploadAjax'// 需要链接到服务器地址  
  5.                     secureuri : false,  
  6.                     fileElementId : 'file'// 文件选择框的id属性  
  7.                     dataType : 'json'// 服务器返回的格式,可以是json  
  8.                     success : function(data, status) // 相当于java中try语句块的用法  
  9.                     {  
  10.                               
  11.                             //alert("成功: " + data.path);  
  12.                             $("#pictureImage1")[0].src = data.path;  
  13.                             $("#pictureImage").val(data.path);  
  14.                               
  15.                     },  
  16.                     error : function(data, status, e) // 相当于java中catch语句块的用法  
  17.                     {  
  18.                             alert("失败");  
  19.                     }  
  20.             });  
  21.     }  


 

注意:这个ajax事件用到了一个JS文件,为:

<script type="text/javascript" src="${contextPath}/js/base/user/ajaxfileupload.js"></script>
这个要在jsp文件头上面进行申明,才可以使用。

3.ajax文件中使用到的文件上传控制器,如下所示:

 

  1. /* 
  2.     * Copyright (C) 2012 GZ-ISCAS Inc., All Rights Reserved. 
  3.     */  
  4.     package cn.ac.iscas.gz.nssc.base.web.user;  
  5.     import java.io.File;  
  6.     import java.io.FileOutputStream;  
  7.     import java.io.IOException;  
  8.     import java.util.HashMap;  
  9.     import java.util.Map;  
  10.     import javax.servlet.http.HttpServletRequest;  
  11.     import javax.servlet.http.HttpServletResponse;  
  12.     import net.sf.json.JSONObject;  
  13.     import org.springframework.stereotype.Controller;  
  14.     import org.springframework.ui.ModelMap;  
  15.     import org.springframework.util.FileCopyUtils;  
  16.     import org.springframework.validation.BindingResult;  
  17.     import org.springframework.web.bind.annotation.ModelAttribute;  
  18.     import org.springframework.web.bind.annotation.RequestMapping;  
  19.     import org.springframework.web.bind.annotation.RequestMethod;  
  20.     import org.springframework.web.bind.annotation.RequestParam;  
  21.     import org.springframework.web.multipart.MaxUploadSizeExceededException;  
  22.     import org.springframework.web.multipart.MultipartFile;  
  23.     import org.springframework.web.servlet.HandlerExceptionResolver;  
  24.     import org.springframework.web.servlet.ModelAndView;  
  25.     import org.springframework.web.servlet.view.RedirectView;  
  26.     import cn.ac.iscas.gz.nssc.base.domain.UploadForm;  
  27.     /** 
  28.     * @ClassName: UploadFormController 
  29.     * @Description: 文件上传控制器controller 
  30.     * @author xuzhongming Email: xuzhongming@gz.iscas.ac.cn 
  31.     * @date 2012-2-23 - 下午3:34:43 
  32.     * @version : 1.0 
  33.     */  
  34.     @Controller  
  35.     @RequestMapping(value = "/FileUploadForm.do")  
  36.     public class UploadFormController {  
  37.             @RequestMapping(params = "method=uploadAjax")  
  38.             public void uploadAjax(@RequestParam(value = "file") MultipartFile file,  
  39.                             HttpServletRequest request, HttpServletResponse response) {  
  40.                     String uploadDirPath = request.getSession().getServletContext()  
  41.                                     .getRealPath("/upload");  
  42.                     MultipartFile image = file;  
  43.                     File destFile = new File(uploadDirPath + "/"  
  44.                                     + image.getOriginalFilename());  
  45.                     try {  
  46.                             FileCopyUtils.copy(image.getBytes(), destFile);  
  47.                     } catch (IOException e) {  
  48.                             e.printStackTrace();  
  49.                     }  
  50.                     String destPath = request.getContextPath() + "/upload/"  
  51.                                     + destFile.getName();  
  52.                     try {  
  53.                             JSONObject jsonObject = new JSONObject();  
  54.                             jsonObject.put("path", destPath);  
  55.                             response.setContentType("text/html");  
  56.                             response.getWriter().println(jsonObject.toString());  
  57.                     } catch (IOException e) {  
  58.                             e.printStackTrace();  
  59.                     }  
  60.             }  
  61.     }  


 

4.在配置文件中注册

 

  1. <!-- Configure the multipart resolver -->  
  2.   <bean id="multipartResolver"  
  3.        class="org.springframework.web.multipart.commons.CommonsMultipartResolver"  
  4.                p:defaultEncoding="utf-8"   
  5.        >  
  6.       <!-- one of the properties available; the maximum file size in bytes -->  
  7.       <property name="maxUploadSize" value="1024000"/>  
  8.   </bean>  

 

 

这样就可以实现文件的上传了。

5.实现效果如下所示:

捕获100.PNG

 



参考地址:
                  http://blog.csdn.net/sundenskyqq/article/details/6799038
                   http://www.open-open.com/home/space-668-do-blog-id-5708.html
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics