Spring Boot实现文件上传和下载

实现Spring Boot文件上传和下载的步骤:

1.文件上传

  • 在pom.xml文件中添加依赖:spring-boot-starter-webspring-boot-starter-thymeleaf
  • 创建一个上传前端的页面,包括一个表单来选择文件和一个提交按钮。
  • 在Controller中添加一个POST方法,该方法接受MultipartFile参数,将文件保存在服务器上。
  • 在application.properties文件中配置上传文件的最大大小和文件存储路径。

2.文件下载

  • 在Controller中添加一个GET方法,该方法接收一个文件名参数,通过ResponseEntity将文件内容返回给客户端。
  • 如果需要授权访问,则可以通过使用Spring Security实现基本身份验证。

以下是Spring Boot实现文件上传和下载的代码示例:

1.文件上传
  • pom.xml依赖配置:
<!-- 文件上传 -->
<dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.3.3</version>
</dependency>

<!-- Servlet API -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>4.0.1</version>
  <scope>provided</scope>
</dependency>
  • 在配置文件中配置:
    # 开启上传和下载
    spring.servlet.multipart.enabled=true
    # 最大的文件大小
    spring.servlet.multipart.max-file-size=30MB
    # 单次最大请求大小
    spring.servlet.multipart.max-request-size=30MB
    # 自定义的上传文件存放路径
    file.upload.dir=e:/test
    
  • 在Controller中添加一个POST方法:
@PostMapping("/upload")
@ResponseBody
public String upload(@RequestParam("file") MultipartFile file) {
    if (file.isEmpty()) {
        return "上传失败,请选择文件";
    }

    // 获取文件名
    String fileName = file.getOriginalFilename();
    // 获取文件的后缀名
    String suffixName = fileName.substring(fileName.lastIndexOf("."));

    // 上传后的路径
    String filePath = "D:/upload/";

    // 解决中文问题,liunx下中文路径,图片显示问题
    fileName = UUID.randomUUID() + suffixName;

    // 构建上传路径
    File dest = new File(filePath + fileName);

    // 检测是否存在目录
    if (!dest.getParentFile().exists()) {
        dest.getParentFile().mkdirs();
    }

    try {
        // 保存文件
        file.transferTo(dest);
        return "上传成功";
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "上传失败";
}
2.文件下载
  • 在Controller中添加一个GET方法:
@GetMapping("/download")
public ResponseEntity<byte[]> download() throws IOException {
    // 获取文件
    File file = new File("D:/upload/1.jpg");

    // 构造响应
    HttpHeaders headers = new HttpHeaders();
    headers.setContentDispositionFormData("attachment", file.getName());
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
}
编写Controller方法
@GetMapping("/download")
public void downloadFile(HttpServletResponse response) {
    try {
        //获取要下载的文件
        File file = new File("file_path");

        //设置响应的内容类型为二进制流,即文件类型
        response.setContentType("application/octet-stream");

        //设置文件名
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));

        //创建输入流
        FileInputStream inputStream = new FileInputStream(file);
        BufferedInputStream buffInputStream = new BufferedInputStream(inputStream);

        //创建输出流
        ServletOutputStream outputStream = response.getOutputStream();
        BufferedOutputStream buffOutputStream = new BufferedOutputStream(outputStream);

        //循环读取数据并写入到响应输出流中
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = buffInputStream.read(buffer)) != -1) {
            buffOutputStream.write(buffer, 0, len);
        }

        //关闭流
        buffOutputStream.flush();
        buffOutputStream.close();
        outputStream.flush();
        outputStream.close();
        buffInputStream.close();
        inputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
SpringBoot中,我们可以通过以下步骤实现文件响应给浏览器实现下载:

在Controller中编写请求处理方法,并使用@GetMapping注解来指定该方法处理GET请求。

在方法中使用InputStreamResource将本地文件以流的形式读取到内存中。

使用ResponseEntity.ok()方法构造响应,并将之前获取到的InputStreamResource作为参数传入。

使用.header()方法设置响应头,包括Content-DispositionContent-Type等信息。

以下是一个示例代码:

import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@Controller
public class FileDownloadController {
    
    @GetMapping("/download")
    public ResponseEntity<InputStreamResource> downloadFile() throws IOException {
        String filename = "example.txt";
        InputStream is = new FileInputStream(filename);
        InputStreamResource resource = new InputStreamResource(is);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment; filename=\"" + filename + "\"");
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        
        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(is.available())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(resource);
    }
}
其中,Content-Disposition头用于描述响应结果的类型,这里指定为attachment表示要下载的文件,filename表示文件名。

Cache-ControlPragmaExpires头用于控制缓存的行为,这里设为不缓存。

最后将InputStreamResource对象作为响应体返回即可。


  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值