我在springboot开发一个电子商务web应用程序。我面临http状态500错误。实际上,我正试图提交一个表单,在其中我上传一个产品图片。所有功能都正常工作,但当我点击submit按钮时,出现一个内部服务器错误,消息如下:
2021-04-21 12:32:18.511 warn 20042---[nio-9111-exec-2].w.s.m.s.defaulthandlerexceptionresolver:已解析[org.springframework.web.method.annotation.methodargumentconversionnotsupportedexception:未能将类型“org.springframework.web.multipart.support.StandardMultipathTtpServletRequest$standardmultipartfile”的值转换为所需类型'java.lang.string';嵌套异常为java.lang.illegalstateexception:无法将类型为“org.springframework.web.multipart.support.StandardMultipartAttpServletRequest$standardmultipartfile”的值转换为所需类型“java.lang.string”:找不到匹配的编辑器或转换策略]
下面是我上传文件的控制器代码。在下面的代码中,我提取了文件名及其扩展名,然后将其转换为字节。
@RequestMapping(value = ApiUrl.uploadfile, method = { RequestMethod.POST, RequestMethod.GET }, produces = {MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<?> uploadfile(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
CustomResponse = ResponseFactory.getResponse(request);
String imgurl = "NA";
try {
String path = Constants.webmedia;
String relativepath = "public/media/";
System.out.println("Here is the image: ");
if (null != file) {
String filename = file.getOriginalFilename();
String extension = filename.substring(filename.lastIndexOf("."), filename.length());
System.out.println("filename : " + filename + " || extension : " + extension);
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
Date date = new Date();
String formatdate = format.format(date);
formatdate = "imgurl" + formatdate;
System.out.println("formatdate : " + formatdate);
byte[] bytes = file.getBytes();
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(path + File.separator + formatdate + extension)));
stream.write(bytes);
stream.flush();
stream.close();
imgurl = relativepath + formatdate + extension;
System.out.println("imgurl : " + imgurl);
if (imgurl != null) {
CustomResponse.setResponse(imgurl);
CustomResponse.setStatus(CustomStatus.OK);
CustomResponse.setStatusCode(CustomStatus.OK_CODE);
}
}
} catch (Exception e) {
e.printStackTrace();
CustomResponse.setResponse(null);
CustomResponse.setStatus(CustomStatus.Error);
CustomResponse.setStatusCode(CustomStatus.Error_CODE);
CustomResponse.setResponseMessage(CustomStatus.ErrorMsg);
}
return new ResponseEntity<ResponseDao>(CustomResponse, HttpStatus.OK);
}
暂无答案!
目前还没有任何答案,快来回答吧!