1

我正在使用此代码上传图片,但我总是得到 415 Unsupported Media Type

@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;
    }
4

2 回答 2

2

添加 servlet-context.xml

<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

在控制器中

公共无效方法(@RequestParam MultipartFile 文件){..}

于 2013-03-18T07:22:09.380 回答
1
于 2013-03-18T06:13:02.200 回答