springMvc的配置文件的代码
使用springMVC包装的解析器(CommonsMultipartResolver)进行文件上传控制 需要引入 apache的 common-fileupload组件包加上common-io
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
<!-- 配置扫描 -->
<context:component-scan base-package="cn.et"></context:component-scan>
<!-- 配置上传的解析器 id是写死的不能改变 -->
<!-- 名称必须使用 multipartResolver 因为spring容器使用名称注入 文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上传的大小限制 -->
<property name="maxUploadSize" value="1048576"></property>
</bean>
</beans>
action的代码
@Controller
@RequestMapping(value="/day0601")
public class UploadingDownloadAction {
//上传的默认路径
private final String FILEPATH="E:/upload";
//下载的默认路径
private final String DOWNFILEPATH="E:/mydownload";
/**
* 文件上传
* @param multipartFile
* @return
* @throws IllegalStateException
* @throws IOException
*/
@RequestMapping(value="/uploading.action")
public String uploading(@RequestParam(name="uploadingFile") MultipartFile multipartFile) throws IllegalStateException, IOException{
//获取上传的文件名
String fileName=multipartFile.getOriginalFilename();
//上传文件的存储路径
String file=FILEPATH+"/"+fileName;
//调用这个方法直接上传
multipartFile.transferTo(new File(file));
return "/day20170601/success.jsp";
}
/**
* 文件下载
SpringMVC使用消息转换器 ByteArrayHttpMessageConverter 设置文件内容为响应体 通过设置响应头ContentDisposition 通知浏览器下载的附件名称
* @return
* @throws Exception
*/
@RequestMapping(value="/download.action")
public ResponseEntity<byte[]> fileDowload(String downloadFile) throws Exception{
//下载文件的路径
String fileName=DOWNFILEPATH+"/"+downloadFile;
//创建这个文件
File file=new File(fileName);
//设置响应头
HttpHeaders hh=new HttpHeaders();
//设置下载的文件的名称
hh.setContentDispositionFormData("attachment", URLEncoder.encode(downloadFile, "UTF-8"));
//读取目标文件为二进制数组
byte[] fileByte=FileCopyUtils.copyToByteArray(file);
//构建ResponseEntity对象
ResponseEntity<byte[]> re=new ResponseEntity<byte[]>(fileByte, hh, HttpStatus.CREATED);
return re;
}
}
jsp的代码
编写jsp文件跳转的动作只能是form表单,并且添加属性enctype="multipart/form-data"必需要是post提交
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'uplodDownl.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${pageContext.request.contextPath}/day0601/uploading.action" method="post" enctype="multipart/form-data">
<input type="file" name="uploadingFile"/>
<input type="submit" value="上传"/>
<a href="${pageContext.request.contextPath}/day0601/download.action?downloadFile=A.txt">下载</a>
</form>
</body>
</html>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- spring自带的解决乱码的过滤器 -->
<filter>
<filter-name>utf</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>utf</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<!-- 配置这个selevlet来加载sprinmvc的配置文件 -->
<filter-name>hidden</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hidden</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 通过这个参数去找配置文件 不加这个参数默认在 /WEB-INF/找spservlet-name-servlet.xml这个文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/springmvc.xml</param-value>
</init-param>
<!-- 启动tomcat的时候就加载 -->
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- /拦截所有以.action结尾的 -->
<url-pattern>*.action</url-pattern>
<!-- /拦截所有servlet -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>