httpClient,在分块模式下执行多部分 POST 时出现问题...

发布于 2024-12-01 18:05:11 字数 998 浏览 3 评论 0原文

好吧,我想知道如何才能以分块模式发布多部分。我有 3 个部分,文件可能很大,因此必须分块发送。

这是我所做的:

    MultipartEntity multipartEntity = new MultipartEntity() {
        @Override
        public boolean isChunked() {
            return true;
        }
    };

    multipartEntity.addPart("theText", new StringBody("some text", Charset.forName("UTF-8")));

    FileBody fileBody1 = new FileBody(file1);
    multipartEntity.addPart("theFile1", fileBody1);

    FileBody fileBody2 = new FileBody(file2);
    multipartEntity.addPart("theFile2", fileBody2);

    httppost.setEntity(multipartEntity);

    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpClient httpClient = new DefaultHttpClient(params);

    HttpResponse httpResponse = httpClient.execute(httppost);

在服务器端,我确实收到了 3 个部分,但文件没有被分块,它们被作为一个整体接收...基本上总共我看到仅出现 4 个边界:3 --xxx, 1 at结束--xxx--。 我认为重写 isChunked 可以解决问题,但没有...;(

我想做的事情可行吗?我怎样才能做到这一点?

非常感谢。 极品

Well I am wondering how I can achieve to post a multipart in chunked mode. I have 3 parts, and the files which can be big so must be sent in chunks.

Here what I do :

    MultipartEntity multipartEntity = new MultipartEntity() {
        @Override
        public boolean isChunked() {
            return true;
        }
    };

    multipartEntity.addPart("theText", new StringBody("some text", Charset.forName("UTF-8")));

    FileBody fileBody1 = new FileBody(file1);
    multipartEntity.addPart("theFile1", fileBody1);

    FileBody fileBody2 = new FileBody(file2);
    multipartEntity.addPart("theFile2", fileBody2);

    httppost.setEntity(multipartEntity);

    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpClient httpClient = new DefaultHttpClient(params);

    HttpResponse httpResponse = httpClient.execute(httppost);

On the server side, I do receive the 3 parts but the files for example are not chunked, they are received as one piece... basically total I see 4 boundaries appearing only : 3 --xxx, 1 at the end --xxx-- .
I thought the override of isChunked would do the trick but no... ;(

Is what I am trying to do feasible ? How could I make that work ?

Thanks a lot.
Fab

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离笑几人歌 2024-12-08 18:05:12

要生成分块的多部分主体,其中一个部分的大小必须不可用。就像正在流式传输的部分一样。

例如,假设您的 file2 是一个非常大的视频。您可以用该代码替换代码的一部分

FileBody fileBody2 = new FileBody(file2);
multipartEntity.addPart("theFile2", fileBody2);

final InputStreamBody binVideo = new InputStreamBody(new FileInputStream(file2), "video/mp4", file2.getName());
multipartEntity.addPart("video", binVideo);

由于现在第三部分是 InputStream 而不是 File,因此您的多部分 HTTP 请求将具有标头 Transfer-Encoding: chunked

To generate a multipart body chunked, one of the part must have it size unavailable. Like a part that is streaming.

For example let assume your file2 is a really big video. You could replace the part of your code:

FileBody fileBody2 = new FileBody(file2);
multipartEntity.addPart("theFile2", fileBody2);

wtih that code:

final InputStreamBody binVideo = new InputStreamBody(new FileInputStream(file2), "video/mp4", file2.getName());
multipartEntity.addPart("video", binVideo);

since now the third part is an InputStream instead of File, your multipart HTTP request will have the header Transfer-Encoding: chunked.

绝影如岚 2024-12-08 18:05:12

通常,任何合适的服务器端 HTTP 框架(例如 Java EE Servlet API)都会隐藏传输细节,例如应用程序代码中的传输编码。仅仅因为您没有通过从内容流读取来看到块分隔符,并不意味着底层 HTTP 传输未使用块编码。

您可以通过激活线路日志记录来准确查看 HttpClient 生成的 HTTP 数据包类型,如下所述:

http://hc.apache.org/httpcomponents-client-ga/logging.html

Usually any decent server-side HTTP framework (such as Java EE Servlet API) would hide transport details such as transfer coding from the application code. just because you are not seeing chunk delimiters by reading from the content stream does not mean the chunk coding was not used by the underlying HTTP transport.

You can see exactly what kind of HTTP packets HttpClient generates by activating the wire logging as described here:

http://hc.apache.org/httpcomponents-client-ga/logging.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文