在Spring中使用ResponseBody上传图像

时间:2013-03-18 05:49:51

标签: java spring-mvc httprequest

我正在使用此代码上传图片,但我一直得到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;
    }

2 个答案:

答案 0 :(得分:2)

添加servlet-context.xml

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

在控制器中

public void方法(@RequestParam MultipartFile文件){..}

答案 1 :(得分:1)