码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMvc (注解)中的上传文件

时间:2016-03-18 21:37:44      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

第一步:导入commons-fileupload-1.3.1.jar 和commons-io-2.2.jar 架包

第二步:在applicationContext.xml中 配置

<bean id=”multipartResolver” class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”/>

第三步:在页面 <form>标签添加属性enctype=”multipart/form-data” 这个是上传文件必须的

具体一个简单方法如下

 1.web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="3.0" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 7   <display-name></display-name>    
 8   <welcome-file-list>
 9     <welcome-file>index.jsp</welcome-file>
10   </welcome-file-list>
11 
12     <!-- 配置pringmvc前端控制器 -->
13     <servlet>
14           <servlet-name>dispatcherServlet</servlet-name>
15           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16       <!-- 设置springmvc的配置文件 -->
17           <init-param>
18             <param-name>contextConfigLocation</param-name>
19             <param-value>/WEB-INF/shopping-servlet.xml</param-value>
20           </init-param>
21           <load-on-startup>1</load-on-startup>
22    </servlet>
23    <servlet-mapping>
24           <servlet-name>dispatcherServlet</servlet-name>
25           <url-pattern>/</url-pattern>
26    </servlet-mapping>
27 </web-app>

2.shopping-servlet.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:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/context
10         http://www.springframework.org/schema/context/spring-context.xsd">
11 
12     <!-- 让spring容器自动探知bean   Controller    Service    Respoise   Componet -->
13     <!-- 
14     <bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
15      -->
16     
17     <context:component-scan base-package="com.bdqnsjz.shopping.controller"></context:component-scan>
18 
19     <bean id="multipartResolver"       
20           class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:maxUploadSize="34564356345456" p:maxUploadSizePerFile="3456435634545" p:defaultEncoding="utf-8">
21           <!-- 
22           <property name="defaultEncoding" value="utf-8"></property>
23            -->
24     </bean>
25 
26 
27     <!-- 配置视图解析器 -->
28     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
29         <property name="prefix" value="/WEB-INF/pages/"></property>
30         <property name="suffix" value=".jsp"></property>
31     </bean>
32 
33     
34 
35 </beans>

3.controller

 1 @RequestMapping(value="/upload1", method=RequestMethod.POST)
 2     public String upload1(@RequestParam(value = "file", required = false) MultipartFile file,
 3             HttpServletRequest request){
 4         
 5         String fileName = file.getOriginalFilename();
 6         System.out.println(fileName);
 7         
 8         //获取扩展名
 9         String extension = fileName.substring(fileName.lastIndexOf("."));
10         
11         //新的文件名    234-135234532l34kjlkj.jpg
12         String savedFileName = java.util.UUID.randomUUID().toString() + extension;
13         
14         String folderPath = request.getRealPath("/WEB-INF/upload");
15         
16         File savedFile = new File(folderPath, savedFileName);
17         
18         
19         //保存文件
20         try {
21             file.transferTo(savedFile);
22         } catch (IOException e) {
23             e.printStackTrace();
24         }
25         
26         return "upload1";
27     }

 

SpringMvc (注解)中的上传文件

标签:

原文地址:http://www.cnblogs.com/Y-S-X/p/5293330.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!