SpringMVC-----文件上传与下载
✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨
🍎合抱之木,生于毫末;百丈之台,起于垒土;千里之行,始于足下。------《老子》
🍊今日学习任务!!!!!
🍊1、文件的下载和上传
🔥 一、文件上传
在 SpringMVC 中提供了用于处理文件上传的组件 CommonsMultipartResolver(多部件解
析器)。可以通过该组件很方便的实现文件上传。该组件的运行需要依赖于 Apache 的
commons-fileupload 与 commons-io 包。
前提准备
<?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_4_0.xsd"
version="4.0">
<!--配置前端控制器-->
<servlet>
<servlet-name>springmvc</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>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<?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: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">
<!-- 配置注解扫描-->
<context:component-scan base-package="com.bjsxt.web.controller"/>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置多部件解析器-->
<!--多部件解析器的id必须为multipartResolver-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件总容量。单位字节 10M = 10*1024*1024-->
<property name="maxUploadSize" value="10485760"/>
<!--设置文件名的编码方式-->
<property name="defaultEncoding" value="utf-8"/>
</bean>
<mvc:default-servlet-handler/>
</beans>
控制类
@RequestMapping("/singleFile")
public String singleFile(MultipartFile file, String username, HttpServletRequest request){
String fileName = UUID.randomUUID().toString()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
String realPath = request.getServletContext().getRealPath("/fileupload");
File temp = new File(realPath,fileName);
try{
file.transferTo(temp);
}catch(Exception e){
e.printStackTrace();
}
return "redirect:/page/ok";
}
index页面
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/8/3 0003
Time: 9:35
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="/file/singleFile" method="post" enctype="multipart/form-data">
用户名:<input type="text" name="username"/><br/>
文件上传:<input type="file" name="file"/><br/>
<input type="submit" value="OKOK"/>
</form>
</body>
</html>
效果图
✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨
🔥 二、文件下载
前提准备:SpringMVC配置文件、前端控制器、jar文件
下载页面
🚨注意、页面最前面需要添加
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/8/3 0003
Time: 14:29
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:forEach items="${files}" var="file">
<a href="/file/fileDown?fileName=${file}">${file}</a><br/>
</c:forEach>
</body>
</html>
控制类
@RequestMapping("/fileDown")
public void fileDown(String fileName, HttpServletRequest request, HttpServletResponse response){
//设置响应头
response.setHeader("Content-Disposition","attachment;filename="+fileName);
//获取绝对路径
String realPath = request.getServletContext().getRealPath("/fileupload");
File file = new File(realPath,fileName);
//获取字节输出流
try{
ServletOutputStream outputStream = response.getOutputStream();
//读文件
outputStream.write(FileUtils.readFileToByteArray(file));
outputStream.flush();
outputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
效果图
✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨