2013-04-08 39 views
7

如何获取我们在页面上为该文件提供链接时上传到服务器上的文件名?如何在webview中点击URL时获取文件名

我正在做的是,我在webview中提供数据与文件链接,所以无论何时用户点击链接需要从服务器下载,因为我已经从服务器下载此文件,但问题无法获得其确切类型和名称使用DownloadManager。我想这样的

enter image description here

见上面我已经给链接,我在test_androt文件,当我点击它会给与具有文件名选项dailog,我不知道如何实现这一目标当用户点击WebView URL链接时的文件名。

编辑 对不起,忘了提,我的URL看起来像这样

misc/dnload.php?t1=MzQ0MDA=&t2=MTY5NTUz&t3=MTY5NTUzMTMwMjEyMDNfcGhhcm1hY3kga2V5IGluZm8ucG5n&t4=MTMwMjEyMDM= 
+2

你做一个HTTP请求来下载该文件。如果是这样,那么你可以从标题内容获取文件名。 – Raghunandan 2013-04-16 04:45:19

回答

9

由于Raghunandan的建议,我得到了答案。

在这里,我需要额外的电话来获取下载文件的标题信息。在标题部分我得到了该文件的名称。

而且我发现这个Filename from URL not containing filename suffix后,通过它我就头的细节,我们可以提出请求时得到更多的细节。

我们可以利用这个URLUtil.guessFileName(url, null, null)但这将给出呼叫装置为我的情况下的文件名,我有这样的

misc/dnload.php?t1=MzQ0MDA=&t2=MTY5NTUz&t3=MTY5NTUzMTMwMjEyMDNfcGhhcm1hY3kga2V5IGluZm8ucG5n&t4=MTMwMjEyMDM= 

URL,以便从上面的链接,这将提取dnload.php为文件名称它不是原始文件名,我们下载它刚创建的那个文件的下载链接。

这里是利用这一点,我们可以得到的文件名,它只是一个额外的调用来获取信息,但实际上我们在网上下载,下载我用的Android内置的API下载管理器下载文件的代码。

Content-Disposition this is the attribute in header section through which we can get the file name as in attachment form. 

它会返回信息像这样Content-Disposition: attachment; filename="fname.ext"所以现在只需要解压的文件名

class GetFileInfo extends AsyncTask<String, Integer, String> 
{ 
    protected String doInBackground(String... urls) 
    { 
       URL url; 
       String filename = null; 
       try { 
        url = new URL(urls[0]); 
       HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
       conn.connect(); 
       conn.setInstanceFollowRedirects(false); 

       String depo = conn.getHeaderField("Content-Disposition"); 
       String depoSplit[] = depo.split("filename="); 
       filename = depoSplit[1].replace("filename=", "").replace("\"", "").trim(); 
       } catch (MalformedURLException e1) { 
        e1.printStackTrace(); 
       } catch (IOException e) { 
       } 
      return filename; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 
    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(); 
     // use result as file name 
    } 
} 
+0

考虑['setRequestMethod(“HEAD”)'](http://developer.android.com/reference/java/net/HttpURLConnection.html#setRequestMethod(java.lang.String))而不是'GET'请求,因为你只想要标题。另外,对于更一般的情况,请记住'depo'可能为空,'depo'可能存在但没有文件名,或者文件名可能由服务器进行URL编码,因为[无处不在的非标准行为Internet Explorer](http://stackoverflow.com/q/93551/687315)。 – user113215 2013-04-18 21:24:45

+0

@Pratik:我已经尝试了上述方法,但在我的情况下它不起作用。你能帮我在这里查询我的查询:http://stackoverflow.com/questions/24577166/get-the-file-image-name-from-the-url-which-does-not-end-with-a-文件名称和分机 – 2014-07-05 19:48:45

10

你可以简单地解析URL并获得在最后一个“/”的文字,并且该文件的名称,或者你可以使用

如下图所示,因为我已经在我的DownloadListener用它下面

URLUtil.guessFileName(url, contentDisposition, mimetype); 

//this stuff goes inside your DownloadListener 
@Override 
public void onDownloadStart(final String url, String userAgent,final String contentDisposition, final String mimetype, long contentLength) 
{ 
    String fileName = URLUtil.guessFileName(url, contentDisposition, mimetype); //returns a string of the name of the file THE IMPORTANT PART 

    //proceed with download 
} 

如果你不想使用它你DownloadListener里面,你可以只是路过“空”在每个参数除了URL中使用它,而不contentDisposition或MIME类型(通过在URL)也。

编辑:这只能如果您有嵌入一个文件名的URL的作品。如果您正在寻找更安全的方法,请参阅上面的Pratik答案。

0

为PRATIK的解决方案的一些改进。

的变化:

1)固定破碎的文件名从提取内容处置

2)添加回调接口

用法:

new GetFileInfo(new GetFileInfoListener() { 
    @Override 
    public void onTaskCompleted(String fileName) { 
     Log.v(TAG, "real filename is " + fileName); 
    } 
}).execute(url); 

代码:

class GetFileInfo extends AsyncTask<String, Integer, String> 
{ 
    public interface GetFileInfoListener { 
     void onTaskCompleted(String result); 
    } 

    private final GetFileInfoListener mListener; 

    public GetFileInfo(GetFileInfoListener listener) { 
     mListener = listener; 
    } 

    protected String doInBackground(String... urls) 
    { 
     URL url; 
     String filename = null; 
     try { 
      url = new URL(urls[0]); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.connect(); 
      conn.setInstanceFollowRedirects(false); 

      String depo = conn.getHeaderField("Content-Disposition"); 

      if (depo != null) 
       filename = depo.replaceFirst("(?i)^.*filename=\"?([^\"]+)\"?.*$", "$1"); 
      else 
       filename = URLUtil.guessFileName(urls[0], null, null); 
     } catch (MalformedURLException e1) { 
      e1.printStackTrace(); 
     } catch (IOException e) { 
     } 
     return filename; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 
    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     if (mListener != null) 
      mListener.onTaskCompleted(result); 
    } 
} 
0

的AsyncTask手工获取文件名:

private static class getFileNameAsync extends AsyncTask<String, Void, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     URL url; 
     String filename = null; 
     HttpURLConnection conn = null; 
     try { 
      url = new URL(params[0]); 
      conn = (HttpURLConnection) url.openConnection(); 
      conn.connect(); 
      conn.setInstanceFollowRedirects(false); 

      try { 
       for(int i = 0; i < 100; i++) 
       { 
        String stringURL = conn.getHeaderField("Location"); 
        if (stringURL != null) { 
         url = new URL(stringURL); 
         conn = (HttpURLConnection) url.openConnection(); 
         conn.connect(); 
         conn.setInstanceFollowRedirects(false); 
        } else { 
         i = 100; 
        } 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      String depo = conn.getHeaderField("Content-Disposition"); 
      if (depo != null) { 
       String depoSplit[] = depo.split(";"); 
       int size = depoSplit.length; 
       for(int i = 0; i < size; i++) 
       { 
        if(depoSplit[i].startsWith("filename=")) 
        { 
         filename = depoSplit[i].replaceFirst("(?i)^.*filename=\"?([^\"]+)\"?.*$", "$1").trim(); 
         i = size; 
        } 
       } 
      } 
     } catch (MalformedURLException e){ 
      e.printStackTrace(); 
     } catch (ProtocolException e){ 
      e.printStackTrace(); 
     } catch (FileNotFoundException e){ 
      e.printStackTrace(); 
     } catch (IOException e){ 
      e.printStackTrace(); 
     } catch (Exception e){ 
      e.printStackTrace(); 
     } finally { 
      if (conn != null) 
       conn.disconnect(); 
     } 
     return filename; 
    } 
    @Override 
    protected void onPostExecute(String filename) { 
     super.onPostExecute(filename); 
    } 
} 

代码:

String url = "Put your url hear"; 
    String fileName = ""; 
    url = url.replace(" ", "%20"); 

    AsyncTask<String, Void, String> asyncTask = new getFileNameAsync(); 
    asyncTask.execute(url); 

    try { 
     fileName = asyncTask.get(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     e.printStackTrace(); 
    } catch (CancellationException e) { 
     e.printStackTrace(); 
    } 
    if ((fileName == null) || (fileName.hashCode() == "".hashCode())) { 
     fileName = URLUtil.guessFileName(url, contentDisposition, mimetype); 
    } 
相关问题