从org.apache.http.HttpResponse获取建议的文件名



在Android中,您可以使用org.apache.httpHttpClientHttpGetHttpResponse下载文件。如何从HTTP请求中读取建议的文件名?

例如,在PHP中,您可以这样做:

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

如何使用Android/Java中的Apache类获取"blah.txt"?

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

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()

相关内容

  • 没有找到相关文章

最新更新