public String uploadPosLogFile(BaseRequest request, MultipartFile file) {
try {
log.info("upload file start.....");
// Determine if the uploaded file is empty
boolean isEmpty = file.isEmpty();
System.out.println("\tisEmpty=" + isEmpty);
log.info(String.valueOf(isEmpty));
if (isEmpty) {
log.info("upload failed,upload file is null!");
throw new RuntimeException("upload failed,upload file is null!");
}
File root = new File("");
String filePath = root.getAbsolutePath()+"\\applog\\";
System.out.println(filePath);
log.info(filePath);
File parent = new File(filePath);
if (!parent.exists()) {
parent.mkdirs();
}
// get original file name
String originalFilename = file.getOriginalFilename();
System.out.println("originalFilename:" + originalFilename);
log.info(originalFilename);
File uploadFile = new File(filePath+originalFilename);
file.transferTo(uploadFile); // upload file
System.out.println(filePath+originalFilename);
log.info(filePath+originalFilename);
log.info("upload file end.....");
return "OK";
}catch (IOException e){
e.printStackTrace();
return "fail";
}
}
ContentType:multipart/form-data
表单提交不要有@ResoponseBody,否则会报以下错误
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data;bounda
其他必须用@ResoponseBody,如:application/x-www-form-urlencoded、application/json,、application/xml等格式
可以通过postman接口进行测试,如下