2012-04-12 7 views
0

サーバークライアントのJSPページでコンテンツサイズをrequest.getContentLength()で取得したいとします。 しかし、request.getContentLength()は常に-1を返します。なぜそうではありませんか?アンドロイドでコンテンツ長を設定する方法は?

Androidのスニペットコード:

URL uri = new URL(actionUrl); 
HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); 
//conn.setChunkedStreamingMode(100); 
conn.setConnectTimeout(setTimeOut>0?setTimeOut:timeoutConnection); 
conn.setReadTimeout(setTimeOut>0?setTimeOut:timeoutConnection); 
conn.setDoInput(true); 
conn.setDoOutput(true); 
conn.setUseCaches(false); 
conn.setRequestMethod("POST"); 
conn.setRequestProperty("Connection", "keep-alive"); 

//conn.setRequestProperty("content-length", "10"); 
//conn.addRequestProperty("content-length", "20"); 
conn.setFixedLengthStreamingMode(30); 

conn.setRequestProperty("Charsert", ENCODING); 
conn.setRequestProperty("Content-Type", "multipart/form-data" 
       + ";boundary=" + java.util.UUID.randomUUID().toString()); 
conn.connect(); 

答えて

4

あなたは、コンテンツ-な長さは、事前に不明な場合に効果的に100バイトのチャンクでchunked transfer encodingを可能にするconn.setChunkedStreamingMode(100)を使用しています。

リクエストの本文に送信するコンテンツの長さを事前に知っている場合は、conn.setFixedLengthStreamingMode(int len)を使用してください。

+0

ありがとう、それは今働くことができます! – boiledwater

関連する問題