springMVC_03文件上传 及 临时目录相关

来源:互联网 发布:ubuntu连打印机 编辑:程序博客网 时间:2024/06/12 12:22

springMVC文件上传

 

增加上传组件依赖:

In the case of the CommonsMultipartResolver, you need to use commons-fileupload.jar.

apache-commons-io.jar

apache-commons-fileupload.jar

 

Xml代码  收藏代码
  1. <!-- fileupload -->  
  2. <dependency>  
  3.     <groupId>commons-io</groupId>  
  4.     <artifactId>commons-io</artifactId>  
  5.     <version>2.4</version>  
  6. </dependency>  
  7. <dependency>  
  8.     <groupId>commons-fileupload</groupId>  
  9.     <artifactId>commons-fileupload</artifactId>  
  10.     <version>1.3</version>  
  11. </dependency>  
 

 

 

 

配置文件上传相关配置,springMVC.xml配置如下:

 

 

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.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  7.     xsi:schemaLocation="        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.         http://www.springframework.org/schema/mvc  
  12.         http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
  13.       
  14.     <!-- 自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter -->  
  15.     <!-- 两个bean 是spring MVC为@Controllers分发请求所必须的  -->  
  16.     <mvc:annotation-driven/>  
  17.   
  18.     <!-- 配置需要被扫描的包 -->  
  19.     <context:component-scan base-package="com.gc.springmvc.controller"/>  
  20.   
  21.     <!-- 配置对静态资源文件的访问不被过滤 WebContent/resources目录中的文件都能访问 -->  
  22.     <mvc:resources location="/resources/" mapping="/resources/**"/>  
  23.       
  24.     <!-- 配置返回的数据如何呈现:前缀+逻辑视图+后缀 -->  
  25.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  26.     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />   
  27.         <property name="prefix" value="/WEB-INF/jsp/"/>  
  28.         <property name="suffix" value=".jsp"/>  
  29.     </bean>  
  30.       
  31.     <!-- 文件上传 -->  
  32.     <bean id="multipartResolver"  
  33.           class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  34.         <!-- Default is ISO-8859-1 -->  
  35.         <property name="defaultEncoding" value="UTF-8"/>  
  36.         <!-- Default is 10240 -->  
  37.         <property name="maxInMemorySize" value="10240"/>  
  38.         <!-- -1 indicates no limit (the default) -->  
  39.         <property name="maxUploadSize" value="100000"/>  
  40.         <!-- javax.servlet.context.tempdir(the default) 临时文件存放目录,不是最终目录!-->  
  41.         <property name="uploadTempDir" value="/tempdir"/>  
  42.     </bean>  
  43.       
  44. </beans>  
 

 

上传页面

Html代码  收藏代码
  1. <form action="fileupload" method="post" enctype="multipart/form-data">  
  2.     文件名称:  
  3.     <input type="text" name="name"/> <br/>  
  4.     选择文件:  
  5.     <input type="file" name="file"/> <br/>  
  6.     <input type="submit"/>      
  7. </form>  

 

上传控制器

Java代码  收藏代码
  1. package com.gc.springmvc.controller;  
  2.   
  3. import java.io.File;  
  4. import java.util.Date;  
  5.   
  6. import javax.servlet.ServletContext;  
  7.   
  8. import org.springframework.stereotype.Controller;  
  9. import org.springframework.web.bind.annotation.RequestMapping;  
  10. import org.springframework.web.bind.annotation.RequestMethod;  
  11. import org.springframework.web.bind.annotation.RequestParam;  
  12. import org.springframework.web.context.ServletContextAware;  
  13. import org.springframework.web.multipart.commons.CommonsMultipartFile;  
  14. import org.springframework.web.servlet.view.InternalResourceViewResolver;  
  15.   
  16. @Controller  
  17. public class FileUploadController implements ServletContextAware{  
  18.       
  19.     private ServletContext servletContext;  
  20.       
  21.     public void setServletContext(ServletContext servletContext) {  
  22.         this.servletContext = servletContext;  
  23.     }  
  24.       
  25.     /** 
  26.      * 文件上传 
  27.      * @param assignedName 为文件指定新的名称 
  28.      * @param file  上传的文件 
  29.      * @return 
  30.      */  
  31.     @RequestMapping(value={"fileupload"}, method=RequestMethod.POST)  
  32.     public String fileupload(@RequestParam("name")String assignedName,@RequestParam("file")CommonsMultipartFile file) {  
  33.         String viewName = "uploadSuccess";  
  34.         if(!file.isEmpty()) {  
  35.             //通过servletContext获取到文件的绝对路径  
  36.             String realPath = servletContext.getRealPath("/upload");  
  37.             System.out.println("文件被保存到:"+realPath);  
  38.               
  39.             System.out.println("指定的名称:"+assignedName);  
  40.               
  41.             String originalFileName = file.getOriginalFilename();  
  42.             System.out.println("原始名称:"+originalFileName);  
  43.               
  44.             String fileType = originalFileName.substring(originalFileName.lastIndexOf("."));  
  45.             System.out.println("文件类型:"+fileType);  
  46.               
  47.             File targetFile = new File(realPath, assignedName+"-"+new Date().getTime()+fileType);  
  48.             try {  
  49.                 file.getFileItem().write(targetFile);  
  50.             } catch (Exception e) {  
  51.                 e.printStackTrace();  
  52.                 viewName = "uploadFailure";  
  53.             };  
  54.         }  
  55.         //重定向  
  56.         return InternalResourceViewResolver.REDIRECT_URL_PREFIX+viewName;  
  57.     }  
  58.       
  59.     /** 
  60.      * 返回成功视图  
  61.      */  
  62.     @RequestMapping(value="uploadSuccess")  
  63.     public String uploadSuccess() {  
  64.         return "uploadSuccess";  
  65.     }  
  66.       
  67.     /** 
  68.      * 返回失败视图  
  69.      */  
  70.     @RequestMapping("uploadFailure")  
  71.     public String uploadFailure() {  
  72.         return "uploadFailure";  
  73.     }  
  74.   
  75. }  
0 0
原创粉丝点击