新建springmvc项目
需要使用的jar包有
配置springmvc.xml和web.xml文件
springmvc.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: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="org.zmt.*"></context:component-scan>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<!--注入视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前后缀-->
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--上传和下载-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="102400000000"></property>
</bean>
</beans>
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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--<load-on-startup>1</load-on-startup>-->
<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>/</url-pattern>
</servlet-mapping>
<!--过滤器-->
<filter>
<filter-name>fileencoding</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>fileencoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
在web下新建文件夹download和upload
前端页面index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
<p>文件上传 <input type="file" name="file"/></p>
<p>文件上传 <input type="file" name="file"/></p>
<p>文件上传 <input type="file" name="file"/></p>
<p><input type="submit" value="上传"/></p>
</form>
<p><a href="download/20220301.rar">下载1</a></p>
<p><a href="download/fj.png">下载2</a></p>
<p><a href="download?fileName=fj.png">下载3</a></p>
</body>
</html>
success.jsp中的内容
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
上传成功
</body>
</html>
controller中的内容
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.util.Date;
import java.util.Random;
@Controller
public class UploadController {
@RequestMapping("/upload")
public String upload(MultipartFile[] file, HttpServletRequest request) throws IOException {
for (int i=0;i< file.length;i++) {
//1.获得上传的路径
String path = request.getServletContext().getRealPath("/upload/");
//2.拿到从页面传过来的文件
String name = file[i].getOriginalFilename();
//3.对上传的文件改名
String newName = new Date().getTime() + new Random(9999999).nextInt() + name;
//4.文件上传路径
File f = new File(path + newName);
//5.上传
file[i].transferTo(f);
}
return "success";
}
@RequestMapping("/download")
public ResponseEntity<byte[]> downloads(@RequestParam("fileName") String fileName,HttpServletRequest request) throws IOException {
//1.下载路径
String path = request.getServletContext().getRealPath("/download/");
//2.下载的完整路径
File f = new File(path+fileName);
//3.转格式
String name = new String(fileName.getBytes("utf-8"),"iso8859-1");
//4.头部信息对象
HttpHeaders hh = new HttpHeaders();
hh.setContentDispositionFormData("attachment",name);
hh.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(f),hh, HttpStatus.CREATED);
}
}
配置服务器测试项目