我正在使用此代码上传图片,但我一直得到415不支持的媒体类型
@RequestMapping(value = "/updateImage" , method = RequestMethod.POST)
public @ResponseBody ResponseMsg uploadFileHandler(
@RequestHeader(value="userid" ,required=false) String userid,
@RequestHeader(value="name" ,required=false) String name,
@RequestHeader(value="phone" ,required=false) Integer phone,
@RequestBody MultipartFile multipartFile,
HttpServletRequest request) throws IllegalStateException, IOException {
System.out.println("hi");
String orgName = multipartFile.getOriginalFilename();
orgName = CommonUtils.createUniqueFileName(orgName);
ServletContext sc = request.getSession().getServletContext();
String filePath = "uploadedImages/" + orgName;
String completePath = sc.getRealPath("/") + "/" + filePath;
File dest = new File(completePath);
multipartFile.transferTo(dest);
ResponseMsg responseMsg = CommonUtils.checkParam(userid, name, phone,filePath);
if (responseMsg.getStatus().equalsIgnoreCase("True"))
responseMsg = userService.updateProfile(userid, name, phone,filePath);
return responseMsg;
}
答案 0 :(得分:2)
添加servlet-context.xml
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
在控制器中
public void方法(@RequestParam MultipartFile文件){..}
答案 1 :(得分:1)
@RequestBody MultipartFile multipartFile
),最好只映射特定的表单字段,例如:@RequestParam('someFileUpload') MultipartFile uploadedFile
,这假设您使用文件选择在您的HTML格式中,例如:<input type="file" name="someFileUpload"/>