首页 > 代码库 > SpringMVC基础(二)采用注解上传文件

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=http://www.mamicode.com/"/">>
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=http://www.mamicode.com/"上传" >>
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";
		}	
	}


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