springmvc文件上传

来源:互联网 发布:cool edit mac 破解版 编辑:程序博客网 时间:2024/05/18 20:07
  1. <!--文件上传的jar包-->  
  2.     <dependency>  
  3.       <groupId>commons-fileupload</groupId>  
  4.       <artifactId>commons-fileupload</artifactId>  
  5.       <version>1.3.1</version>>  
  6.     </dependency>  
  7.   
  8.     <dependency>  
  9.       <groupId>commons-io</groupId>  
  10.       <artifactId>commons-io</artifactId>  
  11.       <version>1.4</version>>  
  12.     </dependency>  
[java] view plain copy
  1. @Controller  
  2. public class Text {  
  3.     @RequestMapping("/first")  
  4.     public String t(MultipartFile upload, HttpSession session){  
  5.           //获取用户上传的文件名称  
  6.         String filename = upload.getOriginalFilename();//文件名称  
  7.         //将相对路径换成绝对路径  
  8.         String realPath = session.getServletContext().getRealPath("/upload");  
  9.         //将 file 写入指定的路径  
  10.   
  11.         File file=new File(realPath,filename);  
  12.   
  13.         try {  
  14.             upload.transferTo(file);  
  15.             return "/welcon.jsp";  
  16.         } catch (IOException e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.         return  "/upload.jsp";  
  20.     }  
  21. }  
[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  xmlns="http://www.springframework.org/schema/beans"  
  3.         xmlns:aop="http://www.springframework.org/schema/aop"  
  4.         xmlns:tx="http://www.springframework.org/schema/tx"  
  5.         xmlns:context="http://www.springframework.org/schema/context"  
  6.         xmlns:p="http://www.springframework.org/schema/p"  
  7.         xmlns:mvc="http://www.springframework.org/schema/mvc"  
  8.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  9.         xsi:schemaLocation="  
  10.         http://www.springframework.org/schema/beans  
  11.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  12.         http://www.springframework.org/schema/aop  
  13.         http://www.springframework.org/schema/aop/spring-aop.xsd  
  14.         http://www.springframework.org/schema/tx  
  15.         http://www.springframework.org/schema/tx/spring-tx.xsd  
  16.         http://www.springframework.org/schema/context  
  17.         http://www.springframework.org/schema/context/spring-context.xsd  
  18.          http://www.springframework.org/schema/mvc  
  19.         http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  20. ">  
  21.     <!--配置 包 扫描器-->  
  22.     <context:component-scan base-package="cn.hello.Upload"/>  
  23.   
  24.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>  
  25.   
  26.    <mvc:annotation-driven/>  
  27.   
  28. </beans>  

[html] view plain copy
  1. <h1>文件上传</h1>  
  2. <form action="/first" method="post" enctype="multipart/form-data">  
  3.     文件   <input type="file" name="upload"/>  
  4.     <input type="submit"/>  
  5. </form>  

[html] view plain copy
  1. <body>  
  2.    欢迎你  
  3. </body>  
原创粉丝点击