SpringBoot 上传文件

目录

1、配置文件上传所需的依赖

2、配置文件上传的文件大小限制

3、单文件上传示例

4、多文件上传示例 


1、配置文件上传所需的依赖

文件上传的依赖,pom.xml内容:

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--@Slf4j日志组件-->
<dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
</dependency>

2、配置文件上传的文件大小限制

application.properties配置文件添加:

# 上传文件总的最大值
spring.servlet.multipart.max-request-size=10MB
# 单个文件的最大值
spring.servlet.multipart.max-file-size=10MB

3、单文件上传示例

创建UploadController控制类,内容如下:

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping
public class UploadController {

    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("uploadFile") MultipartFile uploadFile) {
        if (uploadFile.isEmpty()) {
            return "上传文件为空,请选择文件";
        }
        String fileName = uploadFile.getOriginalFilename();
        String filePath = "D:/uploadfiles/"; //本地测试路径
        File dest = new File(filePath + fileName);
        try {
            //使用组件上传
            uploadFile.transferTo(dest);
            log.info("文件:{},上传成功", fileName);
            return "上传成功!";
        } catch (IOException e) {
            log.error(e.toString(), e);
        }
        return "上传失败!";
    }
}

使用postman测试接口

4、多文件上传示例 

多文件上传只是比单文件上传多了File选择的Input而已,UploadController控制类的变动如下:

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping
public class UploadController {
    // 方式一:使用HttpServletRequest接收参数
    @PostMapping("/multiUpload")
    @ResponseBody
    public String multiUpload(HttpServletRequest request) {
        List<MultipartFile> uploadFiles = ((MultipartHttpServletRequest) request).getFiles("uploadFile");
        String filePath = "D:/uploadfiles/";
        for (int i = 0; i < uploadFiles.size(); i++) {
            MultipartFile file = uploadFiles.get(i);
            int index = i; //记录上传的文件
            if (file.isEmpty()) {
                return "上传第" + (++index) + "个文件失败";
            }
            String fileName = file.getOriginalFilename();
            File dest = new File(filePath + fileName);
            try {
                file.transferTo(dest);
                log.info("第" + (++index) + "个文件上传成功");
            } catch (IOException e) {
                log.error(e.toString(), e);
                return "上传第" + (++index) + "个文件失败";
            }
        }
        return "上传成功!";
    }

    // 方式二:直接使用MultipartFile[]数组接收参数
    @PostMapping("/import")
    public String multiUpload2(@RequestParam("uploadFile") MultipartFile[] files) throws Exception {
        String filePath = "D:/uploadfiles/";
        List<MultipartFile> multipartFiles = Arrays.asList(files);
        //使用stream不方便跟踪具体文件信息
        multipartFiles.stream().forEach((file) -> {
            if (file.isEmpty()) {
                throw new RuntimeException("文件内容为空!");
            }
            String fileName = file.getOriginalFilename();
            File dest = new File(filePath + fileName);
            try {
                file.transferTo(dest);
            } catch (IOException e) {
                throw new RuntimeException(e); //只能抛出RuntimeException
            }
        });
        return "文件上传成功!";
    }
}

使用postman测试接口

 查看本地上传结果:

参考文章:

Spring Boot教程(十三):Spring Boot文件上传_蝈蝈的博客-CSDN博客_springboot文件上传

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

swadian2008

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

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

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

打赏作者

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

抵扣说明:

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

余额充值