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";
}
}
参考文章链接:https://blog.youkuaiyun.com/qq_41389354/article/details/111406845
完.