SpringBoot文件上传和下载

Controller:


@RestController
@Api(value = "文件上传下载相关接口")
@RequestMapping(value = "/file", produces = "application/json;charset=utf-8")
public class FileController {

    private static final Logger log = LoggerFactory.getLogger(FileController.class);

    @ApiOperation("单文件上传")
    @PostMapping
    public JsonResult<Void> fileUpload(MultipartFile file) throws IOException {
        if (null == file) {
            log.error("上传文件不能为空");
            return new JsonResult<>("4", "上传文件不能为空!");
        }
        String fileName = file.getOriginalFilename();
        byte[] bytes = file.getBytes();
        log.info("开始上传文件【{}】...", fileName);
        FileUtils.writeByteArrayToFile(new File("d:/" + fileName), bytes);
        log.info("文件【{}】上传成功...", fileName);
        return new JsonResult<>();
    }

    @ApiOperation("多文件上传")
    @PostMapping("/batch")
    public JsonResult<Void> batchFileUpload(HttpServletRequest request) throws IOException {
        List<MultipartFile> fileList = ((MultipartHttpServletRequest) request).getFiles("file");
        for (int i = 0; i < fileList.size(); i++) {
            MultipartFile file = fileList.get(i);
            byte[] bytes = file.getBytes();
            log.info("开始上传文件【{}】...", i);
            FileUtils.writeByteArrayToFile(new File("d:/temp" + file.getOriginalFilename()), bytes);
            log.info("文件【{}】上传成功...", i);
        }
        return new JsonResult<>();
    }

    @ApiOperation("文件下载")
    @GetMapping("/download")
    public JsonResult<Void> download(HttpServletResponse response) throws UnsupportedEncodingException {
        String fileName = "干净桌面.jpg";
        // 设置强制下载不打开
        response.setContentType("application/force-download");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"),"ISO-8859-1"));
//        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] buff = FileUtils.readFileToByteArray(new File("C:\\Users\\Administrator\\Pictures\\电脑桌面\\" + fileName));
            outputStream.write(buff);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new JsonResult<>();
    }
}

Freemarker:

<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>File</title>
</head>
<body>
    <p>Get your greeting <a href="/greeting">here</a></p>
    <form action="/upload" method="POST" enctype="multipart/form-data">
        文件:<input type="file" name="test"/>
        <input type="submit"/>
    </form>
    <a href="/file/download">下载test</a>
    <p>多文件上传</p>
    <form method="POST" enctype="multipart/form-data" action="/file/batch">
        <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>

配置文件application.yml:

#设置激活的环境 dev
spring:
  profiles:
      active: dev
#freemarker
  freemarker:
    template-loader-path: classpath:/templates
    #设置禁用模板引擎缓存
    cache: false
    settings:
      template_update_delay: 0
#静态文件
  mvc:
    static-path-pattern: /static/**
#文件上传设置
  http:
    multipart:
      max-file-size: 100MB
      max-request-size: 100MB

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员青戈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值