SpringMVC 中文件上传 MultipartResolver

基于前面文章的基础上。

一、准备

    需要的jar

    

 二、配置

  1、  spmvc-servlet.xml

   

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.         xmlns:context="http://www.springframework.org/schema/context"  
  5.         xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.         xmlns:util="http://www.springframework.org/schema/util"   
  7.         xsi:schemaLocation="  
  8.           http://www.springframework.org/schema/beans  
  9.           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  10.           http://www.springframework.org/schema/context  
  11.           http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  12.           http://www.springframework.org/schema/mvc      
  13.           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
  14.           http://www.springframework.org/schema/util   
  15.           http://www.springframework.org/schema/util/spring-util-3.0.xsd">  
  16.        
  17.     <!-- 默认的注解映射的支持 ,它会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter-->   
  18.     <mvc:annotation-driven />  
  19.       
  20.     <!-- 自动扫描注解的Controller -->  
  21.     <context:component-scan base-package="com.wy.controller" />  
  22.       
  23.     <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />  
  24.       
  25.     <!-- 映射处理器 -->  
  26.     <bean id="simpleUrlMapping"  
  27.         class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
  28.         <property name="mappings">  
  29.             <props>  
  30.                 <prop key="/fileUploadController.do">fileUploadController</prop>  
  31.             </props>  
  32.         </property>  
  33.     </bean>  
  34.       
  35.     <!-- ParameterMethodNameResolver 解析请求参数,并将它匹配Controller中的方法名 -->  
  36.     <bean id="parameterMethodNameResolver"  
  37.         class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">  
  38.         <property name="paramName" value="action" />  
  39.     </bean>  
  40.       
  41.     <bean id="fileUploadController"  
  42.         class="com.wy.controller.FileUploadController">  
  43.         <property name="methodNameResolver"  
  44.             ref="parameterMethodNameResolver">  
  45.         </property>  
  46.     </bean>  
  47.       
  48.     <!-- 文件上传表单的视图解析器 -->  
  49.     <bean id="multipartResolver"    
  50.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
  51.         <!-- one of the properties available; the maximum file size in bytes -->    
  52.         <property name="maxUploadSize" value="204800" />    
  53.     </bean>    
  54.        
  55. </beans>            

 

2、Controller

  使用两种方式:

       一种是基于注解的,另一种传统的方式HttpServletRequest

      使用第二种方式时要注意:操作方法中对应的方法参数前两位必须是request,response对象并且都要加上,否则会出现 No request handling method with name 'insert' in class  "ClassName",页面显示为404错误
这个问题出现在使用多操作控制器情况下,相关的操作方法中对应的方法参数前两位必须是request,response对象,必须要有,否则会报如上异常。

 

Java代码   收藏代码
  1. package com.wy.controller;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7. import javax.servlet.http.HttpSession;  
  8.   
  9. import org.springframework.stereotype.Controller;  
  10. import org.springframework.util.MultiValueMap;  
  11. import org.springframework.web.bind.annotation.RequestMapping;  
  12. import org.springframework.web.bind.annotation.RequestMethod;  
  13. import org.springframework.web.bind.annotation.RequestParam;  
  14. import org.springframework.web.multipart.MultipartFile;  
  15. import org.springframework.web.multipart.MultipartHttpServletRequest;  
  16. import org.springframework.web.servlet.ModelAndView;  
  17. import org.springframework.web.servlet.mvc.multiaction.MultiActionController;  
  18.   
  19. @Controller  
  20. @RequestMapping("/fileUploadController")  
  21. public class FileUploadController extends MultiActionController {  
  22.   
  23.     /** 
  24.      * 1、文件上传 
  25.      * @param request 
  26.      * @param response 
  27.      * @return 
  28.      */  
  29.     public ModelAndView uploadFiles(HttpServletRequest request, HttpServletResponse response) {  
  30.         ModelAndView mav = new ModelAndView();  
  31.         // 转型为MultipartHttpRequest  
  32.         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;  
  33.         // 获得上传的文件(根据前台的name名称得到上传的文件)  
  34.         MultiValueMap<String, MultipartFile> multiValueMap = multipartRequest.getMultiFileMap();  
  35.         List<MultipartFile> file = multiValueMap.get("clientFile");  
  36.         //MultipartFile file = multipartRequest.getFile("clientFile");  
  37.         if(!file.isEmpty()){  
  38.             //在这里就可以对file进行处理了,可以根据自己的需求把它存到数据库或者服务器的某个文件夹  
  39.             System.out.println("================="+file.get(0).getName() + file.get(0).getSize());  
  40.         }  
  41.            
  42.         return mav;  
  43.     }  
  44.   
  45.     /** 
  46.      *  
  47.      * @param name 
  48.      * @param file 
  49.      * @param session 
  50.      * @return 
  51.      */  
  52.     @RequestMapping(value="/uploadFile", method=RequestMethod.POST)     
  53.     public String uploadFile(@RequestParam("fileName") String fileName,     
  54.             @RequestParam("clientFile") MultipartFile clientFile, HttpSession session){     
  55.         if (!clientFile.isEmpty()) {  
  56.             //在这里就可以对file进行处理了,可以根据自己的需求把它存到数据库或者服务器的某个文件夹  
  57.             System.out.println("================="+clientFile.getSize());     
  58.         }     
  59.         return "";     
  60.     }    
  61.   
  62. }  

 

3、试图

   upload.jsp

  

Html代码   收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3.     String path = request.getContextPath();  
  4.     String basePath = request.getScheme() + "://"  
  5.             + request.getServerName() + ":" + request.getServerPort()  
  6.             + path + "/";  
  7. %>  
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11.     <head>  
  12.         <title>file upload test</title>  
  13.     </head>  
  14.     <body>  
  15.   
  16.         <form method="post" action="<%=path %>/fileUploadController/uploadFile" enctype="multipart/form-data">  
  17.             文件名: <input type="text" name="fileName" /><br/>  
  18.             &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
  19.             <input type="file" name="clientFile" /><br/>  
  20.             <input type="submit" value="上传文件 "/>  
  21.         </form>  
  22.     </body>  
  23. </html>  

 

  

=================转载http://zachary-guo.iteye.com/blog/1294443====================

 

HTML 页面中的表单最初所采用 application/x-www-form-urlencode 编码方式,并不满足文件上传的需要,所以,RFC 1867 在此基础上增加了新的 multipart/form-data 编码方式以支持基于表单的文件上传。通常情况下,按照如下形式声明表单以及表单中的元素:

Html代码   收藏代码
  1. <form action="..." method="post" enctype="multipart/form-data">  
  2.   <input type="file" name="tile2upload" />  
  3.   <input type="submit" value="Upload" />  
  4. </form>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值