【问题标题】:Filter HTTPEntity output to include new lines过滤 HTTPEntity 输出以包含新行
【发布时间】:2018-05-22 21:52:06
【问题描述】:

我正在使用 org.apache.http.HttpEntity 进行多部分/表单数据 POST 到 HTTPURLConnection 以上传文件。

这是我正在使用的代码。

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
String part1 = "\n{\"name\":\"test.txt\",\"creationTime\":1527023510389,\"fileUri\":\"/storage/test.txt\"}";
File file = new File("/storage/test.txt");

HttpEntity entity = MultipartEntityBuilder.create()
                .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                .addBinaryBody("data", part1.getBytes(), ContentType.APPLICATION_JSON, "data.txt")
                .addBinaryBody("file", file, ContentType.TEXT_PLAIN, filename)
                .setBoundary(boundaryString)
                .build();
OutputStream os = conn.getOutputStream();
entity.writeTo(os);

我看到正文发布如下。

--BOUNDARY
Content-Disposition: form-data; name="metadata"; filename="metadata.txt"
Content-Type: application/json 
{"name":"test.txt","creationTime":1527023510389,"fileUri":"/storage/test.txt"}
--BOUNDARY
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain; charset=ISO-8859-1
test file contents
--BOUNDARY--

问题是服务器需要在Content-Type 和第一部分的内容之间换行。我尝试在开头的内容中添加额外的"\n"(如图所示,但在使用HttpEntity.writeto() 时会被删除。

我想要的输出如下:

--BOUNDARY
Content-Disposition: form-data; name="metadata"; filename="metadata.txt"
Content-Type: application/json 

{"name":"test.txt","creationTime":1527023510389,"fileUri":"/storage/test.txt"}
--BOUNDARY
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain; charset=ISO-8859-1

test file contents
--BOUNDARY--

我试图修改重写输出,但不确定这是否是通过存储在临时文件中的最佳方法。如果这有什么不同的话,我将使用的文件最大为 20mb。

entity.writeTo(new FileOutputStream("file.tmp"));

BufferedReader reader = new BufferedReader(new FileReader("file.tmp"));
OutputStream os = conn.getOutputStream();
PrintWriter writer = new PrintWriter(new BufferedOutputStream(os));
String str;
while ((str = reader.readLine()) != null) {
    writer.println(str);
    if (str.contains("Content-Type: ")) {
        writer.println("\n");
    }
}
writer.close();
reader.close();
os.close();

conn.connect();

if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
    // It's failing when accessing the above method
}

我尝试运行上述代码,但出现以下错误:

java.lang.IllegalStateException: state: 2
    at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:234)
    at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:104)
    at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:1156)
    at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:976)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:509)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:438)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:567)
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java)

【问题讨论】:

    标签: java android io


    【解决方案1】:

    事实证明,HttpEntity.writeTo 方法正在添加必要的新行,但是当我将输出打印到 System.out 时,Android Studio 的 Logcat 没有显示普通的新行。我通过打开我在上面创建的file.tmp 确认了这一点,其中有正确的新行。由于正文对服务器有效,因此请求似乎还有其他错误。

    编辑:在我的请求中发现错误。我没有设置 Content-Type (我想我在删除其他代码时删除了它)。我最终使用它来设置内容类型。

    conn.addRequestProperty(entity.getContentType().getName(), entity.getContentType().getValue());
    

    【讨论】:

      猜你喜欢
      • 2019-06-16
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      • 2022-11-22
      • 2022-11-27
      相关资源
      最近更新 更多