HttpURLConnection downloaded file name

前端 未结 4 1047
栀梦
栀梦 2020-12-29 08:19

Is it possible to get the name of a file downloaded with HttpURLConnection?

URL url = new URL(\"http://somesite/getFile?id=12345\");
HttpURLConnection conn =         


        
4条回答
  •  醉酒成梦
    2020-12-29 09:21

    The frank answer is - unless the web server returns the filename in the Content-Disposition header, there isn't a real filename. Maybe you could set it to the URI's last portion after the /, and before the query string.

    Map m =conn.getHeaderFields();
    if(m.get("Content-Disposition")!= null) {
     //do stuff
    }
    

提交回复
热议问题