httpClient,在Chunked模式下对Multipart进行POST的问题... [英] httpClient, problem to do a POST of a Multipart in Chunked mode...

查看:869
本文介绍了httpClient,在Chunked模式下对Multipart进行POST的问题...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我想知道如何在分块模式中发布多部分。我有3个部分,而且大的文件必须以块的形式发送。

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.

我在这里做的事情:

    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个--xxx--。
我认为isChunked的覆盖可以解决这个问题,但是没有......;(

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 ?

非常感谢。
Fab

Thanks a lot. Fab

推荐答案

要生成分块的多部分主体,其中一个部分必须使其大小不可用。就像流媒体部分一样。

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

例如假设你的文件2非常大您可以替换代码的一部分:

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);

该代码:

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

因为现在第三部分是一个InputStream而不是File,你的多部分HTTP请求将具有标题 Transfer-编码:分块

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

这篇关于httpClient,在Chunked模式下对Multipart进行POST的问题...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆