HttpURLConnection downloaded file name

前端 未结 4 1040
栀梦
栀梦 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 08:57

    Map map = connection.getHeaderFields ();
                if ( map.get ( "Content-Disposition" ) != null )
                {
                    String raw = map.get ( "Content-Disposition" ).toString ();
                    // raw = "attachment; filename=abc.jpg"
                    if ( raw != null && raw.indexOf ( "=" ) != -1 )
                    {
                        fileName = raw.split ( "=" )[1]; // getting value after '='
                        fileName = fileName.replaceAll ( "\"", "" ).replaceAll ( "]", "" );
                    }
                }
    

提交回复
热议问题