2010-12-30 5 views
0

httppostを使用して50MBのファイルをWebサーバーに転送する方法。 ファイルを転送しようとすると、メモリエラーが表示されます。 チャンクされたエンコードを使用できますか?どうやって?いくつかのコードスニペットを与える。android httpclient

ありがとうございます。

編集: は、ここでは、コードです:それはメモリ内のファイル全体をロードしないよう

InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(f), -1); 
reqEntity.setContentType("binary/octect-stream"); 
reqEntity.setChunked(true); 
httppost.setEntity(reqEntity); 
+1

あなたは現在どのような方法を使用していますか? – Mudassir

+0

InputStreamEntity reqEntity = new InputStreamEntity(新しいFileInputStream(f)、-1); reqEntity.setContentType( "binary/octect-stream"); reqEntity.setChunked(true); httppost.setEntity(reqEntity); – umayal

答えて

4

InputStreamEntityを使用してください。

HttpClient httpclient = new DefaultHttpClient(); 

HttpPost httppost = new HttpPost("http://localhost/upload"); 

File file = new File("/path/to/myfile"); 
FileInputStream fileInputStream = new FileInputStream(file); 
InputStreamEntity reqEntity = new InputStreamEntity(fileInputStream, file.length()); 

httppost.setEntity(reqEntity); 
reqEntity.setContentType("binary/octet-stream"); 
HttpResponse response = httpclient.execute(httppost); 
HttpEntity responseEntity = response.getEntity(); 

if (responseEntity != null) { 
    responseEntity.consumeContent(); 
} 

httpclient.getConnectionManager().shutdown(); 
+0

ありがとうございます。しかし、Webサーバー(struts2)でファイルを取得するには – umayal

+0

Struts File Upload Interceptorを使用してください:https://cwiki.apache.org/WW/file-upload-interceptor.html –

関連する問題