SpringMVC基础(二)采用注解上传文件

Spring通过对Servlet API的HttpServletRequest接口进行扩展,使其能够很好地出来文件上传。扩展后的接口名为:org.springframework.web.multipart.MultipartHttpServletRequest。

同时需要注意的是,上传文件无论什么框架都需要用到"enctype"上传方式必须采用method方式。 

上传两个jar包:

    com.springsource.org.apache.commons.fileupload

    com.springsource.org.apache.commons


web.xml

  <servlet>
  	<servlet-name>springMVC</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  		<init-param>
  			<param-name>contextConfigLocation</param-name>
  			<param-value>classpath*:config/springAnnotation-servlet.xml</param-value>
  		</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  
 	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<!-- encoding filter for jsp page -->
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
  
    <servlet-mapping>
  	<servlet-name>springMVC</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>

springAnnotation-servlet.xml文件

  <!-- 注解扫描包 -->
    <context:component-scan base-package="com.tgb.web.controller.annotation" />
    <!-- 开启注解 -->    
    <mvc:annotation-driven/>    

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->  
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
          <property name="defaultEncoding" value="utf-8" />
             <!-- 指定所上传文件的总大小。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->  
          <property name="maxUploadSize" value="10485760000" />
          <property name="maxInMemorySize" value="40960" />
    </bean>

jsp页面:

<body>
	<h>添加用户</h>
	<form name="userForm" action="/springMVC7/file/upload2" method="post" enctype="multipart/form-data" >
		选择文件:<input type="file" name="file">
		<input type="submit" value="上传" >
	</form>
</body>

controller:

	@RequestMapping("/upload2")
		public String upload2(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException{
			
			//定义一个解释器,解析request,拿到springMVC初始化的上下文,解析上下文
			CommonsMultipartResolver multipartResolver  = new CommonsMultipartResolver(request.getSession().getServletContext());
			
			//判断request是否为map类型数据
			if(multipartResolver.isMultipart(request)){
				//转换SpringMVC封装好的文件类型
				MultipartHttpServletRequest  multiRequest = (MultipartHttpServletRequest)request;
				//定义迭代器获取文件名称
				Iterator<String>  iter = multiRequest.getFileNames();
				while(iter.hasNext()){
						//获取文件,MultipartFile定义文件类型
						MultipartFile file = multiRequest.getFile((String)iter.next());
					if(file != null){
						//定义输出文件名称
						String fileName = "demoUpload" + file.getOriginalFilename();
						//定义输出路径
						String path = "D:/" + fileName;		
						//new 一个文件类型
						File localFile = new File(path);
						//将上传文件转到本地文件
						file.transferTo(localFile);
					}
					
				}			
			}
			return "/success";
		}	
	}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。