java spring boot 单/多文件上传/下载

使用版本

后端

  • spring-boot 3.3.0
  • jdk17

前端

  • vue “^3.3.11”
  • vite “^5.0.8”
  • axios “^1.7.2”

文件上传

上传文件比较简单。一般前端传文件流(二进制)到后端,后端处理文件流保存到目标位置即可!

服务端

MultipartFile是SpringMVC提供简化上传操作的工具类。
主要是使用 MultipartFile 的 transferTo 方法。

这里使用了MultipartFile[] 表示支持多文件上传

@PostMapping(path = {"/upload"})
public void getMs(@RequestPart("file") MultipartFile[] files) throws IOException {
    for (MultipartFile file : files){
        String fileName = file.getOriginalFilename();
        File dest = new File("/Users/cyq/Downloads/" + fileName);
        file.transferTo(dest);
    }
}

客户端(前端)

方式一

使用原生上传

需要注意的是

  • 用formData去保存文件信息,
  • 设置类型’Content-Type’: ‘multipart/form-data’

formData可以存储二进制文件、blob等类型,

<script setup>
import { ref } from 'vue'
import axios from 'axios'

function sendRequest(file) {
  const formData = new FormData();

  formData.append('file', file[0]);
  formData.append('file', file[1]);

  axios.post('/api/ceel/hi', formData,{
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
    .then(function (response) {
      console.log(response);
    })
    .catch(function (error) {
      console.log(error);
    });
}

function getFile(event){
  const files =  event.target.files
  console.log(files);
  sendRequest(files)
}
</script>

<template>
  <input v-on:change="getFile" multiple="multiple" type="file" />
</template>

方式二

就很简单了,直接使用elment-plus的上传组件。
使用这种方式多文件上传时- 其实是一个一个的上传的。并不是一下子上传。

<script setup>
import { ref } from 'vue'
const fileList = ref([])
</script>

<template>
<el-upload multiple
   action="/api/ceel/hi"
   v-model:file-list="fileList"
>
	<el-button type="primary">上传文件</el-button>
</el-upload>
</template>


文件下载

下载文件一般都是处理文件流。
通常使用一个byte数组(字节数组)来存放文件流中的数据,每次存取byte数组的长度个数据。然后放到输出流中。
重复以上动作,直到文件流处理完成!

就像是个搬运工,每次搬运指定字节的数据,从输入流到输出流直到搬完。

服务端

@GetMapping("/download")
public void download(String fileName, HttpServletResponse response) throws IOException {

    String _u = "/Users/cyq/Downloads/";
    String filePath = _u + fileName + ".xlsx";
    File file = new File(filePath);

    response.setContentType("application/octet-stream");
    // 告知浏览器文件大小
    response.addHeader("Content-Length", "" + file.length()); 
    response.setHeader("content-disposition","attachment;fileName="+ URLEncoder.encode(file.getName(), "UTF-8"));

    FileInputStream inputStream = new FileInputStream(file);
    ServletOutputStream outputStream = response.getOutputStream();
    try (inputStream; outputStream){
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) > 0){
            outputStream.write(buffer, 0, len);
        }
    }
}

客户端(前端)

发起请求,需要明确返回数据的类型是 blob,添加responseType: ‘blob’
拿到返回流后,通过URL.createObjectURL处理文件流,生成一个url,供a标签进行下载!
下载完成后需要移除。

function sendRequest() {
  axios.get('/api/ceel/download?fileName=模板-财源系统', {
    responseType: 'blob'
  })
    .then(function (response) {
      const url = window.URL.createObjectURL(new Blob([response.data]));
      const link = document.createElement('a');
      link.href = url;
      link.setAttribute('download', '模板-财源系统.xlsx');
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    })
    .catch(function (error) {
      console.log(error);
    });
}

代码仓库地址

  • 后端代码 https://github.com/Mrceel/java-demo.git

路径为

package com.example.practicejava.file;
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,为了实现文件上传功能,需要几步操作: 1. 在 `pom.xml` 中添加文件上传所需的依赖,如 `commons-fileupload` 和 `commons-io` 2. 在 `application.properties` 中配置文件上传的相关参数,如文件上传路径 3. 在Controller 中添加上传文件的接口 4. 在service层添加上传文件的实现逻辑 具体实现过程如下: 1. 在 `pom.xml` 中添加文件上传所需的依赖,如下: ```xml <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.8.0</version> </dependency> ``` 2. 在 `application.properties` 中配置文件上传的相关参数,如文件上传路径,如下: ``` file.upload-dir=D:\\file\\ ``` 3. 在Controller 中添加上传文件的接口,如下: ```java @Controller @RequestMapping("/file") public class FileUploadController { @Autowired private FileUploadService fileUploadService; @PostMapping("/upload") public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { fileUploadService.uploadFile(file); redirectAttributes.addFlashAttribute("message", "You successfully uploaded " + file.getOriginalFilename() + "!"); return "redirect:/file/uploadStatus"; } @GetMapping("/uploadStatus") public String uploadStatus() { return "uploadStatus"; } } ``` 4. 在service层添加上传文件的实现逻辑,如下: ```java @Service public class FileUploadServiceImpl implements FileUploadService { @Value("${file.upload-dir}") private String UPLOAD_DIR;

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值