栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot实战:文件上传下载,代码精简(附git源码)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot实战:文件上传下载,代码精简(附git源码)

Springboot文件上传下载demo,附源码下载
    • 简介
    • 1.依赖导入
        • 1.1 pom添加hutool工具依赖
    • 2. 文件上传下载
        • 2.1 FileUtils 工具类封装
        • 2.2 编写controller层代码
    • 3 效果演示
        • 3.1 用postman工具测试上传接口
        • 3.2 文件下载效果
    • 4 源码下载


简介

本博客项目源码地址:

  • 项目源码github地址
  • 项目源码国内gitee地址
1.依赖导入 1.1 pom添加hutool工具依赖
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.projectlombok
            lombok
            true
        
        
        
            cn.hutool
            hutool-all
            5.7.22
        
2. 文件上传下载 2.1 FileUtils 工具类封装
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;



@Slf4j
public class FileUtils {

    
    public static void saveFile(MultipartFile[] files, String path) {
        for (MultipartFile file : files
        ) {
            saveFile(file, path);
        }
    }

    
    public static void saveFile(MultipartFile file, String path) {
        try {
            // 文件夹不存在则创建
            if (!FileUtil.isDirectory(path)) {
                FileUtil.mkdir(path);
            }
            file.transferTo(new File(path + "/" + file.getOriginalFilename()));
            log.info("文件上传成功--{}", file.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
            log.error("文件上传失败--{}", e.getLocalizedMessage());
        }
    }

    
    public static void getInputStream(final HttpServletResponse response, String path) {
        File file = new File(path);
        if (!FileUtil.isFile(file)) {
            throw new FileNotFoundException();
        }
        String fileName = file.getName();
        // 清空缓冲区,状态码和响应头(headers)
        response.reset();
        // 设置ContentType,响应内容为二进制数据流,编码为utf-8,此处设定的编码是文件内容的编码
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("content-Type", "application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        // 实现文件下载
        BufferedInputStream bis = FileUtil.getInputStream(path);
        try {
            // 往响应体中写入数据
            OutputStream os = response.getOutputStream();
            IoUtil.copy(bis, os, IoUtil.DEFAULT_BUFFER_SIZE);
            log.info("{} 文件下载成功", fileName);
        } catch (Exception e) {
            log.error("{} 文件下载失败", fileName);
        }
    }
}

2.2 编写controller层代码
import lombok.extern.slf4j.Slf4j;
import m.links.file.utils.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;



@RestController
@RequestMapping("file")
@CrossOrigin(origins = "*")
@Slf4j
public class IoFileController {

    @Value("${file.path}")
    private String path;

    @PostMapping("upload")
    public String uploadFile(@RequestParam("files") MultipartFile[] files) {
        FileUtils.saveFile(files, path);
        return "文件上传成功!";
    }

    @GetMapping(value = "/download", consumes = MediaType.ALL_VALUE)
    public void downloadFile(HttpServletResponse response, String fileName) throws IOException {
        FileUtils.getInputStream(response, path + "/" + fileName);
    }

}
3 效果演示 3.1 用postman工具测试上传接口

3.2 文件下载效果

4 源码下载
  • Springboot开发脚手架,集合各种常用框架使用案例,完善的文档,致力于让开发者快速搭建基础环境并让应用跑起来。
  • 项目源码国内gitee地址
  • 项目源码github地址
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/880009.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号