6

In Android, è possibile scaricare un file utilizzando le classi org.apache.httpHttpClient, HttpGet e HttpResponse. Come posso leggere il nome file suggerito dalla richiesta HTTP?Ottieni il nome file suggerito da org.apache.http.HttpResponse

E.g. In PHP, si dovrebbe fare questo:

header('Content-Disposition: attachment; filename=blah.txt'); 

Come faccio ad avere il "blah.txt" utilizzando le classi Apache in Android/Java?

risposta

7
BasicHeader header = new BasicHeader("Content-Disposition", "attachment; filename=blah.txt"); 
HeaderElement[] helelms = header.getElements(); 
if (helelms.length > 0) { 
    HeaderElement helem = helelms[0]; 
    if (helem.getName().equalsIgnoreCase("attachment")) { 
     NameValuePair nmv = helem.getParameterByName("filename"); 
     if (nmv != null) { 
      System.out.println(nmv.getValue()); 
     } 
    } 
} 

sysout> blah.txt

3
HttpResponse response = null; 
try { 
    response = httpclient.execute(httppost); 
} catch (ClientProtocolException e) { 
} catch (IOException e) { 
} 

//observe all headers by this 
Header[] h = response.getAllHeaders(); 
for (int i = 0; i < h.length; i++) { 
    System.out.println(h[i].getName() + " " + h[i].getValue()); 
} 

//choose one header by giving it's name 
Header header = response.getFirstHeader("Content-Disposition"); 
String s = header.getValue()