2010-12-30 2 views
0

Comment transférer un fichier 50mb vers un serveur web en utilisant httppost. Lorsque vous essayez de transférer le fichier, il affiche une erreur de mémoire. pouvons-nous utiliser l'encodage chuncked? Comment? donne des extraits de code.android httpclient

Merci.

Edit: Voici le code:

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

Quelle méthode utilisez-vous maintenant? – Mudassir

+0

InputStreamEntity reqEntity = new InputStreamEntity (nouveau FileInputStream (f), -1); reqEntity.setContentType ("binaire/octect-stream"); reqEntity.setChunked (true); httppost.setEntity (reqEntity); – umayal

Répondre

4

Utilisation InputStreamEntity car il ne se charge pas en mémoire tout le fichier. Faites quelque chose comme ceci:

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

Merci., Mais comment puis-je obtenir le fichier sur le serveur Web (Struts2) – umayal

+0

Utilisez Struts File Upload Interceptor: https://cwiki.apache.org/WW/file-upload-interceptor.html –