Get suggested file name from org.apache.http.HttpResponse

On Android, you can download the file using the org.apache.http HttpClient , HttpGet and HttpResponse classes. How can I read the suggested file name from an HTTP request?

eg. In PHP, you would do the following:

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

How do I get "blah.txt" using Apache classes in Android / Java?

+6
source share
2 answers
 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

+7
source
 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 name Header header = response.getFirstHeader("Content-Disposition"); String s = header.getValue() 
+3
source

Source: https://habr.com/ru/post/911034/


All Articles