Spring MVC与Android文件上传MultipartException

时间:2015-04-07 06:18:08

标签: java android spring spring-mvc

我想将文件从android上传到spring。 将文件从android上传到spring时出错。

错误:servlet调度程序抛出异常:org.springframework.web.multipart.MultipartException:当前请求不是多部分请求

我的Android代码是:

 HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
  HttpPost post = new HttpPost(Constants.ROOTURL+"/media/uploadUserImage");
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("userMO", jsonUserMo));
        post.setEntity(new FileEntity(profileImage,"image/jpeg"));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = client.execute(post);

我的春天代码:

@RequestMapping(value = { "/uploadUserImage" }, method = RequestMethod.POST)
public @ResponseBody
String uploadUserImage(@RequestParam(value = "uploadImg") MultipartFile file, @RequestParam("userMO") String userBO, HttpSession session, HttpServletRequest httpServletRequest) {
    log.info("hitting image");
    UserBO userBo = gson.fromJson(userBO, UserBO.class);
    // jboss file location to store images
    String filePath = httpServletRequest.getSession().getServletContext().getRealPath("/") + "\\resources\\userImages\\" + userBo.getRingeeUserId() + ".png";
    String fileName = file.getOriginalFilename();
    try {
        if (!file.isEmpty() && file.getBytes().length >= 5242880) {
        log.info("file size is "+file.getBytes());
        }
        if (!file.isEmpty()) {
            BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
            BufferedImage resizedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
            // resizedImage = originalImage.getSubimage(x1, y1, w, h);
            File destination = new File(filePath);
            // save cropped image
            ImageIO.write(resizedImage, "jpeg", destination);
        }
    } catch (Exception Exp) {
        log.info("Upload image failure");
    }
    return "";
}

对那一个有任何想法.. 请帮帮我?

0 个答案:

没有答案