springMVC_03文件上传 及 临时目录相关
2016-09-14 13:14
295 查看
springMVC文件上传
增加上传组件依赖:
In the case of the
apache-commons-io.jar
apache-commons-fileupload.jar
Xml代码
<!-- fileupload -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
配置文件上传相关配置,springMVC.xml配置如下:
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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">
<!-- 自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter -->
<!-- 两个bean 是spring MVC为@Controllers分发请求所必须的 -->
<mvc:annotation-driven/>
<!-- 配置需要被扫描的包 -->
<context:component-scan base-package="com.gc.springmvc.controller"/>
<!-- 配置对静态资源文件的访问不被过滤 WebContent/resources目录中的文件都能访问 -->
<mvc:resources location="/resources/" mapping="/resources/**"/>
<!-- 配置返回的数据如何呈现:前缀+逻辑视图+后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- Default is ISO-8859-1 -->
<property name="defaultEncoding" value="UTF-8"/>
<!-- Default is 10240 -->
<property name="maxInMemorySize" value="10240"/>
<!-- -1 indicates no limit (the default) -->
<property name="maxUploadSize" value="100000"/>
<!-- javax.servlet.context.tempdir(the default) 临时文件存放目录,不是最终目录!-->
<property name="uploadTempDir" value="/tempdir"/>
</bean>
</beans>
上传页面
Html代码
<form action="fileupload" method="post" enctype="multipart/form-data">
文件名称:
<input type="text" name="name"/> <br/>
选择文件:
<input type="file" name="file"/> <br/>
<input type="submit"/>
</form>
上传控制器
Java代码
package com.gc.springmvc.controller;
import java.io.File;
import java.util.Date;
import javax.servlet.ServletContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Controller
public class FileUploadController implements ServletContextAware{
private ServletContext servletContext;
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
/**
* 文件上传
* @param assignedName 为文件指定新的名称
* @param file 上传的文件
* @return
*/
@RequestMapping(value={"fileupload"}, method=RequestMethod.POST)
public String fileupload(@RequestParam("name")String assignedName,@RequestParam("file")CommonsMultipartFile file) {
String viewName = "uploadSuccess";
if(!file.isEmpty()) {
//通过servletContext获取到文件的绝对路径
String realPath = servletContext.getRealPath("/upload");
System.out.println("文件被保存到:"+realPath);
System.out.println("指定的名称:"+assignedName);
String originalFileName = file.getOriginalFilename();
System.out.println("原始名称:"+originalFileName);
String fileType = originalFileName.substring(originalFileName.lastIndexOf("."));
System.out.println("文件类型:"+fileType);
File targetFile = new File(realPath, assignedName+"-"+new Date().getTime()+fileType);
try {
file.getFileItem().write(targetFile);
} catch (Exception e) {
e.printStackTrace();
viewName = "uploadFailure";
};
}
//重定向
return InternalResourceViewResolver.REDIRECT_URL_PREFIX+viewName;
}
/**
* 返回成功视图
*/
@RequestMapping(value="uploadSuccess")
public String uploadSuccess() {
return "uploadSuccess";
}
/**
* 返回失败视图
*/
@RequestMapping("uploadFailure")
public String uploadFailure() {
return "uploadFailure";
}
}
增加上传组件依赖:
In the case of the
CommonsMultipartResolver, you need to use
commons-fileupload.jar.
apache-commons-io.jar
apache-commons-fileupload.jar
Xml代码
<!-- fileupload -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
配置文件上传相关配置,springMVC.xml配置如下:
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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">
<!-- 自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter -->
<!-- 两个bean 是spring MVC为@Controllers分发请求所必须的 -->
<mvc:annotation-driven/>
<!-- 配置需要被扫描的包 -->
<context:component-scan base-package="com.gc.springmvc.controller"/>
<!-- 配置对静态资源文件的访问不被过滤 WebContent/resources目录中的文件都能访问 -->
<mvc:resources location="/resources/" mapping="/resources/**"/>
<!-- 配置返回的数据如何呈现:前缀+逻辑视图+后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- Default is ISO-8859-1 -->
<property name="defaultEncoding" value="UTF-8"/>
<!-- Default is 10240 -->
<property name="maxInMemorySize" value="10240"/>
<!-- -1 indicates no limit (the default) -->
<property name="maxUploadSize" value="100000"/>
<!-- javax.servlet.context.tempdir(the default) 临时文件存放目录,不是最终目录!-->
<property name="uploadTempDir" value="/tempdir"/>
</bean>
</beans>
上传页面
Html代码
<form action="fileupload" method="post" enctype="multipart/form-data">
文件名称:
<input type="text" name="name"/> <br/>
选择文件:
<input type="file" name="file"/> <br/>
<input type="submit"/>
</form>
上传控制器
Java代码
package com.gc.springmvc.controller;
import java.io.File;
import java.util.Date;
import javax.servlet.ServletContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Controller
public class FileUploadController implements ServletContextAware{
private ServletContext servletContext;
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
/**
* 文件上传
* @param assignedName 为文件指定新的名称
* @param file 上传的文件
* @return
*/
@RequestMapping(value={"fileupload"}, method=RequestMethod.POST)
public String fileupload(@RequestParam("name")String assignedName,@RequestParam("file")CommonsMultipartFile file) {
String viewName = "uploadSuccess";
if(!file.isEmpty()) {
//通过servletContext获取到文件的绝对路径
String realPath = servletContext.getRealPath("/upload");
System.out.println("文件被保存到:"+realPath);
System.out.println("指定的名称:"+assignedName);
String originalFileName = file.getOriginalFilename();
System.out.println("原始名称:"+originalFileName);
String fileType = originalFileName.substring(originalFileName.lastIndexOf("."));
System.out.println("文件类型:"+fileType);
File targetFile = new File(realPath, assignedName+"-"+new Date().getTime()+fileType);
try {
file.getFileItem().write(targetFile);
} catch (Exception e) {
e.printStackTrace();
viewName = "uploadFailure";
};
}
//重定向
return InternalResourceViewResolver.REDIRECT_URL_PREFIX+viewName;
}
/**
* 返回成功视图
*/
@RequestMapping(value="uploadSuccess")
public String uploadSuccess() {
return "uploadSuccess";
}
/**
* 返回失败视图
*/
@RequestMapping("uploadFailure")
public String uploadFailure() {
return "uploadFailure";
}
}
相关文章推荐
- springmvc上传临时文件目录配置
- springmvc上传临时文件目录配置
- springmvc上传临时文件目录配置
- SpringMVC第五篇【方法返回值、数据回显、idea下配置虚拟目录、文件上传】
- springmvc和servlet下的文件上传和下载(存文件目录和存数据库Blob两种方式)
- 大文件上传代码,根据下拉列表框选中的市县镇名保存文件到指定目录下,并将文件相关信息写进数据库
- tomcat文件上传与存储(虚目录、临时存储)
- springmvc 异常处理、文件上传以及拦截器相关知识
- Java 上传文件过程及清空相关缓存目录
- springmvc实现文件上传到工程目录
- springmvc 文件上传到服务器的固定目录中
- 29 SpringMVC 上传文件未生成临时文件
- SpringMVC--上传功能失败,控制台提示找不到文件临时路径(解决办法)
- 在apache配置文件中设置php上传临时目录
- springmvc 上传图片生成临时文件的问题
- struts2文件上传,设置临时目录和限制文件大小 以及 批量上传
- SpringMVC实现文件上传到Tomcat指定目录(包含pojo提交)
- 160920、springmvc上传图片不生成临时文件
- iPhone app 获取目录、读写临时文件的相关操作
- [导入].net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等