6

HTTPヘッダーでチャンク化されたファイルを送信することでご存知かもしれませんが、コンテンツの長さはないため、プログラムはファイルが終了したことを理解するために0を待つ必要があります。

--sample http header  
POST /some/path HTTP/1.1
Host: www.example.com  
Content-Type: text/plain  
Transfer-Encoding: chunked  
25  
This is the data in the first chunk  
8
sequence  
0

このファイルを受信するには、次のコードを使用できます。

ResponseHandler<String> reshandler = new ResponseHandler<String>() {
    public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {

        HttpEntity entity = response.getEntity();

        InputStream in =  entity.getContent();
        byte[] b = new byte[500];
        StringBuffer out = new StringBuffer();
        int len = in.read(b);
        out.append(new String(b, 0 , len));

        return out.toString();
    }
}; 

しかし、私の場合、ストリーミングチャネルを使用しています。つまり、ファイルが終了したことを示す0はありません。とにかく、私がこのコードを使用する場合、それは決して起こらない0を待つために永遠に待つようです。私の質問は、ストリームチャネルからチャンクファイルを受信するためのより良いアプローチはありますか?

4

1 に答える 1

1

Ok。単純なアプローチを使用してデータを受信できました(次のように、応答ハンドラーを使用せずに)。とにかく、 apache ChunkedInputStreamと、通常のinputstreamがチャンク化されたデータを処理できるのに、それがどのように 役立つかについては、まだ少し混乱してい ます。

is =  entity.getContent();
StringBuffer out = new StringBuffer();
byte[] b = new byte[800];
int len = is.read(b);
out.append(new String(b, 0 , len));
result = out.toString();
于 2011-08-22T01:18:32.923 に答える