我尝试使用邮递员将图像上传到服务器。我使用spring来制作其余的api如下:
@RequestMapping(value = "/uploadPrescription", method =RequestMethod.POST)
public ResponseEntity<ResponseSuccessData> uploadPatientPrescription(
@RequestBody @RequestParam("image") MultipartFile image)
throws IOException {
但它引发了一个错误:
org.springframework.web.bind.MissingServletRequestParameterException:
Required MultipartFile parameter 'image' is not present
正如你在邮递员中看到的那样,关键名称是'image',而在api中,api也是@RequestParam(“image”)。
在内容类型中设置值 - Content-type = multipart / form-data,boundary =' - abc'
这是我的多部分弹簧配置 -
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
//commonsMultipartResolver.setMaxUploadSize(-1);
return commonsMultipartResolver;
}
可能是什么问题?
答案 0 :(得分:3)
答案 1 :(得分:2)
@RestController
public class UserOfferController {
// upload image
@RequestMapping(value = "/uploadimage", method = RequestMethod.POST)
public ResponseEntity<ResponseObjectBean> uploadFile(@RequestParam("uploadedFile") MultipartFile file) {
int statusCode;
String msg;
Object data = null;
long maxsize = configuredValue.getFileMaxAcceptedSize();
if (!file.isEmpty()) {
String name = file.getOriginalFilename();
String imagePath = "path to save your image ";
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(imagePath)));
stream.write(bytes);
statusCode = 200;
msg = "DONE";
data = true;
} catch (Exception e) {
e.printStackTrace();
statusCode = 500;
msg = "FAIL";
data = false;
}
} else {
statusCode = 500;
msg = "FAIL";
data = false;
}
responseData.setStatusCode(statusCode);
responseData.setStatusMsg(msg);
responseData.setData(data);
return new ResponseEntity<ResponseObjectBean>(responseData, HttpStatus.OK);
}
}
在spring.xml中添加这些行
<!-- mutipart upload configuration -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- max upload size in bytes -->
<property name="maxUploadSize" value="1024" />
<!-- max size of file in memory (in bytes) -->
<property name="maxInMemorySize" value="2048" />
</bean>
答案 2 :(得分:1)
除了Abhijit Chowdhury的答案之外,如果您仍然使用Spring安全性,则可以删除Content-Type
并将标记保留在标题中,无需从标题中删除所有内容。
此外,重新开始邮递员很重要。
答案 3 :(得分:0)
1.删除POSTMAN中的标题部分。
2.在您的API中:
@RequestMapping(value = "/uploadPrescription", method =RequestMethod.POST)
public ResponseEntity<ResponseSuccessData> uploadPatientPrescription(
@RequestBody @RequestParam("image") MultipartFile image)
throws IOException {}
添加以下内容:
consumes = MediaType.MULTIPART_FORM_DATA_VALUE
所以它变成了:
@RequestMapping(value = "/uploadPrescription", method =RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<ResponseSuccessData> uploadPatientPrescription(
@RequestBody @RequestParam("image") MultipartFile image)
throws IOException {}
答案 4 :(得分:-1)
将@RequestBody @RequestParam("image")
替换为@RequestBody("image")
。
第一个语句无效,请参阅 - Spring uploading files。