编译工具:IntelliJ IDEA 2019.3.5

代码--------------------------------------------------------------------------------------------
package cn.hp.UploadDownload;
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 UploadDownload {
@RequestMapping("/upload")
public String upload(MultipartFile file[], HttpServletRequest request) throws IOException {
String path = request.getServletContext().getRealPath("/upload/");
for (int i=0; i<file.length;i++){
String name = file[i].getOriginalFilename();
String newName=new Date().getTime()+new Random().nextInt(999999)+name;
File f=new File(path+newName);
file[i].transferTo(f);
}
return "success";
}
@RequestMapping("/download.do")
public ResponseEntity<byte[]>download(@RequestParam("fileName")String fileName,HttpServletRequest request ) throws IOException {
String path=request.getServletContext().getRealPath("/download/");
File f=new File(path+fileName);
String newName=new String(fileName.getBytes("utf-8"),"iso8859-1");
HttpHeaders hh=new HttpHeaders();
hh.setContentDispositionFormData("attachment",newName);
hh.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(f),hh, HttpStatus.CREATED);
}
}
--------------------------------------------------------------------------------------------------------

代码--------------------------------------------------------------------------------------------
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="upload" 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="file" name="file"/><br/>
文件上传<input type="file" name="file"/><br/>
<input type="submit" value="确定"/>
</form>
<a href="download/0.png">下载文件png</a>
<a href="download/name.txt">下载文件txt</a>
<a href="download/test4.txt">下载文件txt</a>
<a href="download/自我介绍.docx">下载文件docx</a>
<a href="download.do?fileName=test4.txt">下载文件</a>
</body>
</html>
--------------------------------------------------------------------------------------------

需要的jar包链接
https://download.csdn.net/download/Taboos_/19465556?spm=1001.2014.3001.5503
spring.xml文件用到的代码部分

代码--------------------------------------------------------------------------------------------
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="10240000"></property>
</bean>
-----------------------------------------------------------------------------------------------------
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>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化时加载springmvc配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</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>
-----------------------------------------------------------------------------------------------------
本文介绍了如何使用SpringMVC框架实现文件上传和下载功能,并结合IntelliJ IDEA 2019.3.5作为开发环境。通过Controller处理POST和GET请求,展示了如何处理MultipartFile,以及创建响应头和返回下载文件。同时,还提供了一个HTML表单示例用于上传多个文件。
UploadDownload&spm=1001.2101.3001.5002&articleId=118637919&d=1&t=3&u=462f5856bbf0453db5b32c54c2e0e18a)
888

被折叠的 条评论
为什么被折叠?



