Spring MVC 上传和下载
一,配置web.xml 和Spring MVC配置文件
首先是我的项目结构:
1,web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--DispatcherServlet 中央核心控制器-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMVC.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
<!--所有.do结尾的控制(请求)-->
</servlet-mapping>
<!--过滤器-->
<filter>
<filter-name>characterencoding</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>characterencoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2,Spring MVC 文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
">
<!--自动扫描上下文包-->
<context:component-scan base-package="cn.tb.*" />
<!--自动开启mvc模式的注解-->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<!--视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<!--注入文件流-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--设置下载文件的编码格式和大小 文件的大小单位是kb-->
<property name="defaultEncoding" value="UTF-8"/>
<!--设置下载文件的大小 文件的大小单位是kb-->
<property name="maxUploadSize" value="10000000"/>
</bean>
</beans>
二,创建jsp前端页面
<%--
Created by IntelliJ IDEA.
User: manager
Date: 2021/3/18
Time: 11:15:34
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%--上传--%>
<form action="upload.do" method="post" enctype="multipart/form-data">
文件上传<input type="file" name="file"/><br/>
文件上传<input type="file" name="file"/><br/>
文件上传<input type="file" name="file"/><br/>
<input type="submit" value="提交">
</form>
<%--下载--%>
<a href="download.do?fileName=1000.jpg">点击下载</a>
</body>
</html>
上传成功后跳转的页面
<%--
Created by IntelliJ IDEA.
User: manager
Date: 2021/3/18
Time: 11:25:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>上传成功页面</title>
</head>
<body>
成功
</body>
</html>
三,创建UploadController控制器
package cn.tb.Test;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Random;
@Controller
public class UploadController {
//上传
@RequestMapping("/upload.do")
public String upload(MultipartFile file[],HttpServletRequest request) throws IOException {
for (int i=0;i<file.length;i++){
//上传路径
String path = request.getServletContext().getRealPath("/upload/");
//获得上传的文件名
String name = file[i].getOriginalFilename();
//改文件名
String newName = new Date().getTime()+new Random().nextInt(999999)+name;
File f1= new File(path+newName);
file[i].transferTo(f1);
}
return "success";
}
//下载
@RequestMapping("/download.do")
public ResponseEntity<byte[]> download(@RequestParam("fileName") String fileName,HttpServletRequest request) throws IOException {
//获得下载路径
String path = request.getServletContext().getRealPath("/download/");
File file = new File(path+fileName);
//转格式
String newName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");
//转流
HttpHeaders hh = new HttpHeaders();
//头部里上下文里的表单数据
hh.setContentDispositionFormData("attachment",newName);
//设置上下文里的数据的数据类型
hh.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),hh, HttpStatus.CREATED);
}
}
注意:在进行下载的时候要注意的一点就是在转流之前,要进行转格式要把UTF-8转换为iso-8859-1,这是要记得的一点。
四,运行结果
1,前端页面
2,上传
选择要上传的文件
上传成功
在项目里显示
3,下载
鼠标点击 “点击下载” 时,在网页左下角显示下载提示
下载成功后,在文件中显示: