博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Springboot2.x实现文件上传下载的功能(非常实用的小例子)
阅读量:4037 次
发布时间:2019-05-24

本文共 5163 字,大约阅读时间需要 17 分钟。

文件的上传下载功能算是一个比较常用的功能,前段时间在着急忙慌的做项目的时候,这部分是由师弟来完成,现在使用Springboot自己总结一下。所有的功能均已测试成功。代码会在评论区给出github地址

非公众号都在评论区给出地址,因为一篇文章会发到我的各个平台,懒得再描述了,公z号的代码地址直接在文末给出。

直接来看步骤。

一、环境搭建

名称 版本
Idea 2018专业版(已破解)
Maven 4.0.0
SpringBoot 2.2.2
jdk 1.8

idea之前发过一次破解码,结果因为违规,文章被删了,这是我群里的一个朋友分享的,亲测可用,2018和2019版本的可以永久破解,需要的可以私信我。

二、整合开发

步骤一:创建Springboot项目,名为SpringbootFile,添加相应的依赖

这一个步骤很简单,不给出具体的实现了。准备工作就是添加依赖

org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-freemarker

步骤二:单文件上传的功能

浏览器有一个文件上传到服务器。因此服务器需要接受处理。这一个步骤的实现很简单,首先我们创建一个包controller,然后再这个包里面创建FileController类

@RestControllerpublic 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"; } }

步骤很明确,首先判断一下传过来的文件是否为空,然后取出文件名和后缀名,最后指定自己的文件路径和刚刚取出的文件名和后缀名进行保存即可。

步骤三:多文件上传的功能

为了实现这个功能,只需要在刚刚那个类新增加一个处理多文件的方法即可。

@RestControllerpublic class FileController {
@PostMapping("/batch") public String handleFileUpload(HttpServletRequest request) {
List
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循环处理,其内部通过输入输出流进行保存到本地。

步骤四:下载文件

我们还在刚刚那个类新增加一个处理下载文件的方法即可。

@RestControllerpublic 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测试了一下均成功。不过为了代码的完整性,还是给出一个前端的代码。

    
${msg}

单文件上传

文件:

文件下载

文件1:

文件2:

上面使用的是模板技术FreeMarker,只需要放在src/main/resources/templates,文件名index.ftl。不过你在使用使用之前需要添加依赖。在一开始已经给出。

转载地址:http://qsbdi.baihongyu.com/

你可能感兴趣的文章
使用 Springboot 对 Kettle 进行调度开发
查看>>
如何优雅的编程,lombok你怎么这么好用
查看>>
一文看清HBase的使用场景
查看>>
除了负载均衡,Nginx还可以做很多,限流、缓存、黑白名单
查看>>
解析zookeeper的工作流程
查看>>
搞定Java面试中的数据结构问题
查看>>
慢慢欣赏linux make uImage流程
查看>>
linux内核学习(7)脱胎换骨解压缩的内核
查看>>
以太网基础知识
查看>>
慢慢欣赏linux 内核模块引用
查看>>
kprobe学习
查看>>
慢慢欣赏linux phy驱动初始化2
查看>>
慢慢欣赏linux CPU占用率学习
查看>>
2020年终总结
查看>>
linux内核学习(4)建立正式内核的页式内存映射, 以x86 32位模式为例
查看>>
Homebrew指令集
查看>>
React Native(一):搭建开发环境、出Hello World
查看>>
React Native(二):属性、状态
查看>>
JSX使用总结
查看>>
React Native(四):布局(使用Flexbox)
查看>>