Spring Boot 文件上传 2021-06-01

Spring Boot 文件上传

有用的代码:

File localFile = new File("d:/" + file.getOriginalFilename());//文件路径加
file.transferTo(localFile);//保存文件

application.yml 内容

server:
  port: 8084
spring:
  servlet:
    multipart:
      max-file-size: 10000MB
      max-request-size: 10000MB
  datasource:
    username: root
    password: ****
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/talkwithus?Timezone=Asia?Shanghai
    

其中
max-file-size: 10000MB 文件上传限制大小
max-request-size: 10000MB 总上传大小

package com.example.demo_uploadfile.UpLoadFileTest;

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.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;


@RequestMapping("/upload")
@RestController
public class UpLoadFileTest0 {

    @PostMapping("/file")
    public Object UpLoad(@RequestParam("file") MultipartFile file) {
        return uploadFile(file);
    }

    private Object uploadFile(MultipartFile file) {
        File localFile = new File("d:/" + file.getOriginalFilename());//文件路径加文件名
        try {
            file.transferTo(localFile);//保存文件
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "ok";
    }
}


运行成功截图

D盘根目录出现了杠杆上传的idea
整个项目结构
参考文章链接:https://blog.csdn.net/qq_41389354/article/details/111406845

完.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值