简体   繁体   English

通过PUT请求上传多部分/表单数据,在okhttp上不起作用?

[英]Multipart/form-data upload by PUT request, doesn't work on okhttp?

I don't know it is bug, or I am doing something wrong. 我不知道这是错误,还是做错了什么。

Here is my okhttp request: 这是我的okhttp请求:

private static final MediaType MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg");

protected void putImage(String path) throws IOException {
    RequestBody requestBody = new MultipartBuilder()
        .type(MultipartBuilder.FORM)
        .addPart(
            Headers.of("Content-Disposition", "form-data; name=\"image\""),
            RequestBody.create(MEDIA_TYPE_JPEG, new File(path)))
        .build();
    Request request = new Request.Builder()
        .url("http://localhost:9090/")
        .put(requestBody)
        .build();
    Timber.d(request.urlString());
    okHttpClient.newCall(request).enqueue(new Callback() {
       @Override public void onFailure(Request request, IOException e) {
           e.printStackTrace();
       }

       @Override public void onResponse(Response response) throws IOException {
           Timber.d(response.body().string());
       }
    });
}

It gives error with at the backend code : 后端代码给出错误:

I thought it just backend code has a bug, however, It works on curl : 我以为只是后端代码有一个错误,但是,它可以在curl

curl -v -XPUT -include --form asdfasdf=@Photo_20140806_000942.jpg http://localhost:9090/

My questions: 我的问题:

  1. Is putImage correct and align with okhttp ? putImage是否正确并与okhttp对齐?
  2. Is my curl command works very intelligently that make it works? 我的curl命令是否非常聪明地工作,使其能够工作?
  3. Or okhttp does extra things that my backend code should handle? 还是okhttp做后端代码应该处理的其他事情?

You might need to specify the filename header. 您可能需要指定文件名标头。 Try setting the Content-Disposition like this: 尝试像这样设置Content-Disposition

Headers.of("Content-Disposition", "form-data; name=\"image\"; filename=\"Photo_20140806_000942.jpg\"")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复请求不包含multipart / form-data或multipart / mixed stream apache错误 - how to fix the request doesn't contain a multipart/form-data or multipart/mixed stream apache error 请求不包含multipart / form-data或multipart / mixed stream apache错误 - the request doesn't contain a multipart/form-data or multipart/mixed stream apache error 请求不包含多部分/表单数据或多部分/混合流,内容类型标头为false - the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is false 请求不包含 multipart/form-data 或 multipart/mixed 流,内容类型标头是 application/x-www-form-urlencoded - the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded 解析 `multipart/form-data` http 请求是如何工作的? - How does parsing the `multipart/form-data` http request work? 使用Camel上传多部分表单数据文件 - Multipart form-data file upload with Camel 骆驼发送多部分/表单数据请求 - Camel send multipart/form-data request 使用 ByteArray 请求 MultiPart/Form-Data - Request MultiPart/Form-Data with ByteArray 如何使用 Java Http 客户端发送具有多部分/表单数据主体的 PUT 请求? - How to send a PUT request with multipart/form-data body with Java Http Client? 如何使用 Spring MockMvc 放置多部分/表单数据? - How to PUT multipart/form-data using Spring MockMvc?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM