springboot 上传文件

此篇博客介绍了如何使用Spring Boot创建RESTful API接口处理文件上传,并管理文件存储路径。作者展示了如何检查文件大小、类型和原始名称,以及创建并确保文件目录的存在。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单文件上传

@RestController
@RequestMapping("/upload")
public class upload {
    Logger log = LoggerFactory.getLogger(upload.class);
    @PostMapping("/file")
    public String upload(@RequestPart("file") MultipartFile multipartFile) {
        log.info(multipartFile.getName());// 获取上传文件前端定义的名称
        log.info(multipartFile.getContentType()); // 文件类型
        log.info(multipartFile.getOriginalFilename());// 文件原始名称
        long fileSize = multipartFile.getSize();
        log.info(fileSize+""); // 文件大小
        log.info(multipartFile.isEmpty()+""); // 文件是否为空
        // 要上传的文件目录
        String path = null;
        // 获取文件的输入流 InputStream inputStream =  multipartFile.getInputStream();
        try {
            if(multipartFile.isEmpty()) {
                return "文件不能为空";
            }
            // /D:/biancheng_files/Maven/spring_boot_01/target/classes/
            // 判断下文件目录是否存在,不存在的话 创建
            File isExistFile = new File(ResourceUtils.getURL("classpath:").getPath()+"static/image");
            if(!isExistFile.exists()) {
                isExistFile.mkdirs();
            }
            path = ResourceUtils.getURL("classpath:").getPath()+"static/image/"+multipartFile.getOriginalFilename();
            // 保存到哪里
            multipartFile.transferTo(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "http://localhost:9000/image/"+multipartFile.getOriginalFilename();
    }
}

2021年12月10日 - 补充:

file.getSize() 拿到的是上传文件的大小,单位是字节(Byte)。

1024Byte = 1kb
1024kb = 1MB

1MB转化为字节: 1MB = 1*1024*1024 = 1048576字节

所有与上传有关的配置:

默认文件大小为:1MB,

 

补充:

也可以使用 FileOutputStream输出流 来保存上传的资源

@RestController
@RequestMapping("/upload")
public class upload {
    Logger log = LoggerFactory.getLogger(upload.class);
    @PostMapping("/file")
    public String upload(@RequestPart("file") MultipartFile multipartFile) {

        // 要上传的文件目录
        String path = null;
        try {
            if(multipartFile.isEmpty()) {
                return "文件不能为空";
            }
            // 判断下文件目录是否存在,不存在的话 创建
            File isExistFile = new File(ResourceUtils.getURL("classpath:").getPath()+"static/image");
            if(!isExistFile.exists()) {
                isExistFile.mkdirs();
            }
            path = ResourceUtils.getURL("classpath:").getPath()+"static/image/"+multipartFile.getOriginalFilename();
            // 创建输出流
            FileOutputStream fos = new FileOutputStream(new File(path), true);
            // 获取 上传资源的字节数组,写到path中
            fos.write(multipartFile.getBytes());
            fos.flush();
            fos.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "http://localhost:9000/image/"+multipartFile.getOriginalFilename();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值