Springboot2.x实现文件上传下载的功能(非常实用的小例
文件的上传下载功能算是一个比较常用的功能,前段时间在着急忙慌的做项目的时候,这部分是由师弟来完成,现在使用Springboot自己总结一下。所有的功能均已测试成功。代码会在评论区给出github地址
非公众号都在评论区给出地址,因为一篇文章会发到我的各个平台,懒得再描述了,公z号的代码地址直接在文末给出。
直接来看步骤。
一、环境搭建

idea之前发过一次破解码,结果因为违规,文章被删了,这是我群里的一个朋友分享的,亲测可用,2018和2019版本的可以永久破解,需要的可以私信我。
二、整合开发
步骤一:创建Springboot项目,名为SpringbootFile,添加相应的依赖
这一个步骤很简单,不给出具体的实现了。准备工作就是添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
步骤二:单文件上传的功能
浏览器有一个文件上传到服务器。因此服务器需要接受处理。这一个步骤的实现很简单,首先我们创建一个包controller,然后再这个包里面创建FileController类
@RestController
public class FileController {
private static final Logger log = LoggerFactory.getLogger(FileController.class);
@RequestMapping(value = "/upload")
public String upload(@RequestParam("file") MultipartFile file) {
try {
if (file.isEmpty()) {
return "file is empty";
}
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
log.info("上传的文件名为:" + fileName+" 后缀名为" + suffixName);
// 设置文件存储路径(G盘),你可以存放在你想要指定的路径里面。
String filePath = "G:\\";
String path = filePath + fileName;
File dest = new File(path);
// 检测是否存在目录
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();// 新建文件夹
}
file.transferTo(dest);// 文件写入
return "upload success";
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "upload failure";
}
}
步骤很明确,首先判断一下传过来的文件是否为空,然后取出文件名和后缀名,最后指定自己的文件路径和刚刚取出的文件名和后缀名进行保存即可。
步骤三:多文件上传的功能
为了实现这个功能,只需要在刚刚那个类新增加一个处理多文件的方法即可。
@RestController
public class FileController {
@PostMapping("/batch")
public String handleFileUpload(HttpServletRequest request) {
List<MultipartFile> files =
((MultipartHttpServletRequest) request).getFiles("file");
MultipartFile file = null;
BufferedOutputStream stream = null;
for (int i = 0; i < files.size(); ++i) {
file = files.get(i);
String filePath = "G:\\uploads";
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
stream = new BufferedOutputStream(new FileOutputStream(
new File(filePath + file.getOriginalFilename())));
stream.write(bytes);// 写入
stream.close();
} catch (Exception e) {
stream = null;
return "the " + i + " file upload failure";
}
} else {
return "the " + i + " file is empty";
}
}
return "upload Multifile success";
}
}
这个步骤很简单,首先通过file参数,拿到多个文件。然后for循环处理,其内部通过输入输出流进行保存到本地。
步骤四:下载文件
我们还在刚刚那个类新增加一个处理下载文件的方法即可。
@RestController
public class FileController {
@GetMapping("/download")
public String downloadFile(HttpServletRequest request,
HttpServletResponse response) {
String fileName = "";// 文件名
if (fileName != null) {
//设置文件路径
File file = new File("G:\\MyProject\\aas.txt");
if (file.exists()) {
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment;fileName="
+ fileName);
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
return "download success";
} catch (Exception e) {
e.printStackTrace();
} finally {
bis.close();
fis.close();
}
}
}
return "failure";
}
}
这段代码其实也比较容易理解,用户点击了下载链接之后,首先服务器设置一下参数,然后使用输出输出流将制定路径下的文件进行输出。
步骤四:测试
我本来想用reactjs建一个文件,出现了跨域问题,懒得写了,就用了postman测试了一下均成功。不过为了代码的完整性,还是给出一个前端的代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> <title>${msg}</title>
</head>
<body>
<p>单文件上传</p>
<form action="upload" method="post" enctype="multipart/form-data">
文件:<input type="file" name="file"/>
<input type="submit"/>
</form>
<hr/>
<p>文件下载</p>
<form action="batch" method="post" enctype="multipart/form-data">
<p>文件1:<input type="file" name="file"/></p>
<p>文件2:<input type="file" name="file"/></p>
<p><input type="submit" value="上传"/></p>
</form>
</body>
</html>
上面使用的是模板技术FreeMarker,只需要放在src/main/resources/templates,文件名index.ftl。不过你在使用使用之前需要添加依赖。在一开始已经给出。
github地址: https://github.com/fengdongdongwsn/MyjavaStudy.git
