2010-12-30 3 views
0

Как перенести файл размером 50 МБ на веб-сервер с помощью httppost. При попытке передать файл в нем отображается ошибка памяти. можно использовать кодировку chuncked? как? дать некоторые фрагменты кода.android httpclient

спасибо.

Edit: Вот код:

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 ("двоичный/octect-поток"); reqEntity.setChunked (истина); 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

Спасибо., Но как я могу получить файл на веб-сервере (struts2) – umayal

+0

Использовать перехватчик загрузки файлов Struts: https://cwiki.apache.org/WW/file-upload-interceptor.html –