springboot上传文件+postman测试
1.创建springboot项目
2.创建controller类,并创建上传接口
package com.qz.testsnykdemo.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.FileCopyUtils;
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.*;
@RestController
@RequestMapping("file")
@Slf4j
public class FileController {
@PostMapping("/uploadFile")
public String getFile(@RequestParam("myfile")MultipartFile file){
if(file.isEmpty()){
log.info("文件为空");
}
//创建输入输出流
InputStream inputStream = null;
OutputStream outputStream = null;
try {
//指定上传的位置为 d:/upload/
String path = "D:\MinZhi\Task26\snykcheck\demox";
//获取文件的输入流
inputStream = file.getInputStream();
//获取上传时的文件名
String fileName = file.getOriginalFilename();
//注意是路径+文件名
File targetFile = new File(path + fileName);
//如果之前的 String path = "d:/upload/" 没有在最后加 / ,那就要在 path 后面 + "/"
//判断文件父目录是否存在
if(!targetFile.getParentFile().exists()){
//不存在就创建一个
targetFile.getParentFile().mkdir();
}
//获取文件的输出流
outputStream = new FileOutputStream(targetFile);
//最后使用资源访问器FileCopyUtils的copy方法拷贝文件
FileCopyUtils.copy(inputStream, outputStream);
} catch (Exception e) {
e.printStackTrace();
//出现异常,则告诉页面失败
} finally {
//无论成功与否,都有关闭输入输出流
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "ok";
}
}
3.启动springboot
4.使用postman测试
5.检查文件是否上传到指定位置