需求后端代码postman模拟页面上传
需求
后端代码
package com.web.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.FileOutputStream;
/**
* @Author: tinker
* @Date: 2022/02/18 10:25
*/
@Slf4j
@RestController
public class FileUploadController {
@PostMapping('/file/upload')
String fileUpload(@RequestParam('file') MultipartFile file) {
String path = 'D:uploadDir';
// 获取文件名(包括后缀)
String fileName = file.getOriginalFilename();
try (FileOutputStream fos = new FileOutputStream(path + fileName)) {
fos.write(file.getBytes());
return '文件上传成功';
} catch (Exception e) {
log.error('file upload failed, fileName = {}', file.getOriginalFilename(), e);
}
return '文件上传失败';
}
}
postman模拟页面上传
step2:点击Body,选择form-data,key为file,之后点击value选择一个文件,文件名为test.txt
文章为作者独立观点,不代表 股票程序化软件自动交易接口观点