ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringBoot 上传文件

2022-03-02 13:03:22  阅读:81  来源: 互联网

标签:文件 SpringBoot web springframework import org 上传


目录

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文件上传

标签:文件,SpringBoot,web,springframework,import,org,上传
来源: https://blog.csdn.net/swadian2008/article/details/123074239

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有