springmvc文件上传
来源:互联网 发布:cool edit mac 破解版 编辑:程序博客网 时间:2024/05/18 20:07
-
- <dependency>
- <groupId>commons-fileupload</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.3.1</version>>
- </dependency>
-
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <version>1.4</version>>
- </dependency>
- @Controller
- public class Text {
- @RequestMapping("/first")
- public String t(MultipartFile upload, HttpSession session){
-
- String filename = upload.getOriginalFilename();
-
- String realPath = session.getServletContext().getRealPath("/upload");
-
-
- File file=new File(realPath,filename);
-
- try {
- upload.transferTo(file);
- return "/welcon.jsp";
- } catch (IOException e) {
- e.printStackTrace();
- }
- return "/upload.jsp";
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc.xsd
- ">
-
- <context:component-scan base-package="cn.hello.Upload"/>
-
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
-
- <mvc:annotation-driven/>
-
- </beans>
- <h1>文件上传</h1>
- <form action="/first" method="post" enctype="multipart/form-data">
- 文件 <input type="file" name="upload"/>
- <input type="submit"/>
- </form>