当我在Spring Boot中使用RestController上传文件时,如下所示,一切都很好:
@PostMapping
public ResponseEntity post(UserCreateRequest request, @RequestPart("file") MultipartFile file) throws IOException {
ftpService.uploadPhoto(file.getOriginalFilename(), file.getInputStream());
return ResponseEntity.ok(userService.create(request));
}
因此,您可以尝试如下更改代码:
@RequestMapping(value = "/schedulebatch",method =
RequestMethod.POST,params="insertData")
public @ResponseBody String addBatch(@RequestPart("upfile") MultipartFile upfile) throws Exception {
if(!upfile.isEmpty()){
System.out.println("test1");
}
}
您的内容类型应该是multipart / form-data,所以我添加了一个html&ajax表单请求示例:
$(document).ready(function () {
$('#btnSubmit').click( function(e) {
e.preventDefault();
var form = $('#my-form')[0];
var data = new FormData(form);
$.ajax({
type: "POST",
enctype: 'multipart/form-data',
url: "http://localhost:8080/schedulebatch",
data: data,
processData: false,
contentType: false,
cache: false,
timeout: 600000,
success: function (data) {
alert("success")
},
error: function (e) {
alert("ERROR : ", e);
}
});
});
});