java - IO异常 : Missing content for multipart request

标签 java android apache-spark heroku okhttp

我正在尝试通过 POST 请求将图像从 Android 应用程序发送到 Heroku 网络服务器。在网络服务器中,我想从请求中检索图像,进行修改,然后将修改后的图像作为响应发回。

但是,我当前的代码在网络服务器中返回一个 IOException

java.io.IOException: Missing content for multipart request org.eclipse.jetty.util.MultiPartInputStreamParser.parse(MultiPartInputStreamParser.java:496)org.eclipse.jetty.util.MultiPartInputStreamParser.getParts(MultiPartInputStreamParser.java:405)

我检查过,userImageFile 至少存在于 Android 应用程序中。

这是我在 Android 应用程序中的代码(使用 OkHttp)。

    //Creating file with the bitmap gotten from the user
    String path = this.getFilesDir().getAbsolutePath();
    File userImageFile = new File(path + "/image.png");
    userImageFile.createNewFile();
    FileOutputStream fop = new FileOutputStream(userImageFile, false);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fop);
    fop.flush();
    fop.close();

    OkHttpClient okHttpClient = new OkHttpClient();
    String url = "https://my-heroku-app-url-here.com/imageConvert";
    RequestBody body = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("image", userImageFile.getName(),
                    RequestBody.create(MediaType.parse("image/png"), userImageFile))
            .build();

    Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();

    Response response = okHttpClient.newCall(request).execute();

这是我的 Heroku 网络服务器代码(使用 Spark 框架)。

post("/imageConvert", (request, response) -> {
        byte[] body = request.bodyAsBytes();
        request.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("/temp"));
        BufferedImage returnImage = null;
        try (InputStream is = request.raw().getPart("image").getInputStream()) {
            BufferedImage userImage = ImageIO.read(is);
            returnImage = getDistortedImage(userImage);
        }catch (IOException ex){
            return "There has been an IO Exception: \n" + ex.getMessage();
        }
        if(returnImage!= null){
            ImageIO.write(returnImage, "png", response.raw().getOutputStream());
            return response.raw();
        }
        return "There was an unknown mistake";
    });

最佳答案

我认为您正在使用请求正文两次。如果请求内容被消费一次,那么如果没有一些明确的重置机制,你就不能再像那样再次消费字节/流。

您应该删除第一个语句,以便第二个语句能够使用输入流。

1. byte[] body = request.bodyAsBytes();  // Remove this

2. try (InputStream is = request.raw().getPart("image").getInputStream()) 

关于java - IO异常 : Missing content for multipart request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53325066/

相关文章:

java - XmlElement 上的 Jaxb/MOXY ClassExtractor

Java hibernate声明连接有效

android - 从 Uri 到 Path 再回到 Uri

android - 以编程方式自动接听来电?

python - 使用数组对象计算 Spark RDD 中的不同文本

java - 无法在 Spark 应用程序中设置系统属性 [在客户端和集群模式下]

apache-spark - 规范化大型 PySpark 数据帧时,CodeGen 增长超过 64 KB 错误

java - Spring Data MongoDB 尝试为自定义存储库方法生成查询

java - 如何像spring mvc一样设置动态变量并将其传递给JSF?

java - 从 Playstore 下载时,专业版应用程序的 apk 大小是免费版应用程序的两倍