SpringBoot下载文件

1 篇文章 0 订阅
1 篇文章 0 订阅
 
 
 
 
 
@Override
public ResponseEntity downloadCase(File file) {
    String downFileName = file.getName();
    try {
        downFileName = new String(file.getName().getBytes("GBK"), Charset.forName("iso-8859-1"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    HttpHeaders headers = new HttpHeaders();
    headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
    headers.add("Content-Disposition", "attachment; filename=" + downFileName);
    headers.add("Pragma", "no-cache");
    headers.add("Expires", "0");
    return ResponseEntity
            .ok()
            .headers(headers)
            .contentLength(file.length())
            .contentType(MediaType.parseMediaType("application/octet-stream"))
            .body(new FileSystemResource(file));
}
@RequestMapping(path = "/upload/files")
public Object uploadFiles(@RequestParam("files") MultipartFile[] files) {
    try {
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            if (fileName.trim().length() == 0) continue;
            FileOutputStream fos = new FileOutputStream(new File(fileName));
            InputStream fis = file.getInputStream();
            FileCopyUtils.copy(fis, fos);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return ReturnResult.FAILUER("上传失败");
    }
    return ReturnResult.SUCCESS("上传成功");
}

@RequestMapping(path = "/upload/file")
public Object uploadFile(@RequestParam("file") MultipartFile file) {
    try {
        String fileName = file.getOriginalFilename();
        if (fileName.trim().length() == 0) {
            return ReturnResult.SUCCESS("上传的文件名为空");
        }
        FileOutputStream fos = new FileOutputStream(new File(fileName));
        InputStream fis = file.getInputStream();
        FileCopyUtils.copy(fis, fos);
    } catch (IOException e) {
        e.printStackTrace();
        ReturnResult.SUCCESS("上传失败");
    }
    return ReturnResult.SUCCESS("上传成功");
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中实现文件下载可以通过以下步骤来完成: 第一步:创建一个Controller类,用于处理文件下载的请求。在该类中,可以使用`@GetMapping`或`@RequestMapping`注解来指定处理的URL路径。然后,在对应的方法中,使用`ResponseEntity`来返回文件内容。例如,可以使用`FileInputStream`将文件读取为字节流,并将其包装在`ResponseEntity`中返回。 第二步:在前端页面中创建一个下载链接或按钮,通过点击该链接或按钮来触发文件下载的请求。可以使用`<a>`标签或`<button>`标签,并设置`href`属性或`onclick`事件来指定下载请求的URL。 下面是一个示例代码,演示了如何在Spring Boot中实现文件下载: ```java @RestController @RequestMapping("/common") public class CommonController { @GetMapping("/download") public ResponseEntity<Resource> downloadFile() throws IOException { // 读取文件内容 File file = new File("path/to/file"); InputStreamResource resource = new InputStreamResource(new FileInputStream(file)); // 设置响应头 HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName()); // 返回文件内容和响应头 return ResponseEntity.ok() .headers(headers) .contentLength(file.length()) .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(resource); } } ``` 在前端页面中,可以使用以下代码来创建一个下载链接: ```html <a href="/common/download">下载文件</a> ``` 当用户点击该链接时,将会触发`/common/download`路径的请求,从而实现文件下载功能。 #### 引用[.reference_title] - *1* *2* *3* [基于springboot文件上传和下载](https://blog.csdn.net/hyt_struggle/article/details/127168991)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值