【问题标题】:Android with spring file uploading MissingServletRequestParameterException带有spring文件上传MissingServletRequestParameterException的Android
【发布时间】:2015-06-29 18:48:55
【问题描述】:

我想将图像从 android 上传到 spring 控制器。

我的问题是我无法上传文件,我在 spring 控制器中出错了

我的安卓代码是:

 public String uploadImageToServer(final UserMO userMO,final Context context,final File profileImage) {
    final String jsonUserMo = gson.toJson(userMO);
    final StringBuilder contactLists = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
    try {
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        FileBody profileFile = new FileBody(profileImage);
        builder.addPart("uploadImg", profileFile);
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("userMO", jsonUserMo));
        HttpPost post = new HttpPost(Constants.ROOTURL+"/media/uploadUserImage");
        String BOUNDARY= "--ringee--";
        post.setHeader("Accept", "application/json");
        post.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
        post.setEntity(builder.build());
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        Log.i("Ringee",post.toString());
        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        contactLists.append(rd.readLine());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return contactLists.toString();
}

我的弹簧控制器代码是:

@RequestMapping(value = { "/uploadUserImage" }, method = RequestMethod.POST)
public @ResponseBody
String uploadUserImage(@RequestParam(value = "uploadImg") MultipartFile file, @RequestParam("userMO") String userBO, HttpSession session,MultipartRequest multipartRequest, 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 "ok";
}

错误:

org.springframework.web.multipart.MultipartRequest,javax.servlet.http.HttpServletRequest)]:
org.springframework.web.ind.MissingServletRequestParameterException:
Required MultipartFile parameter 'uploadImg' is not present

如何解决这个问题请帮帮我?

【问题讨论】:

    标签: java android spring spring-mvc servlets


    【解决方案1】:

    请更新代码:

         HttpClient client = new DefaultHttpClient();
         HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            builder.addPart("uploadImg", new FileBody(profileImage));
            builder.addPart("userMO",new StringBody(jsonUserMo, ContentType.TEXT_PLAIN));
            HttpPost post = new HttpPost(Constants.ROOTURL+"/media/uploadUserImage");
            post.setEntity(builder.build());
            HttpResponse response = client.execute(post)
    

    【讨论】:

      猜你喜欢
      • 2013-08-08
      • 2014-08-01
      • 2011-05-11
      • 2018-07-27
      • 2013-01-02
      • 2013-04-03
      • 2019-10-14
      • 1970-01-01
      • 2018-01-22
      相关资源
      最近更新 更多