2014-11-06 32 views
0

我試圖使用下面的代碼來下載並最終查看PDF文件。該文件的URL是這樣的:嘗試從空格的URL下載PDF時FileNotFoundExecption

http://www.example.com/directory/something.example.com This File.pdf

我已經試過%20替換空間,我已經試過「UrlEncoder.encode」,不管是什麼,我得到任何FileNotFoundException異常或MalformedURLException的(當編碼URL)。例如例外:

java.io.FileNotFoundException: http://www.example.com/directory/something.example.com This File.pdf

java.io.FileNotFoundException: http://www.example.com/directory/something.example.com%20This%20File.pdf

java.net.MalformedURLException: Protocol not found: http%3A%2F%2Fwww.example.com%directory%2Fsomething.example.com+This+File.pdf

如果我這些路徑複製到它的下載罰款的任何瀏覽器。如果服務器有錯誤代碼響應

 File file; 
     try 
     { 
      String urlString = 
       "http://www.example.com/directory/something.example.com This File.pdf" 
      URL url = new URL(urlString); 
      //URL url = new URL(URLEncoder.encode(urlString, "UTF-8")); 
      HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.setDoOutput(true); 
      urlConnection.connect(); 

      file = new File(getExternalFilesDir(null), "test.pdf"); 
      FileOutputStream fileOutput = new FileOutputStream(file); 
      InputStream inputStream = urlConnection.getInputStream(); 

      byte[] buffer = new byte[1024 * 1024]; 
      int bufferLength; 
      while ((bufferLength = inputStream.read(buffer)) > 0) 
      { 
       fileOutput.write(buffer, 0, bufferLength); 
      } 

      fileOutput.flush(); 
      fileOutput.close(); 
      inputStream.close(); 
      return file.getAbsolutePath(); 
     } 
     catch (Exception e) 
     { 
      Log.e(getClass().getSimpleName(), e.getMessage(), e); 
      return ""; 
     } 
+0

你用'+'替換了空格嗎? – laalto 2014-11-06 16:58:50

+1

當我在瀏覽器中嘗試時,這甚至不起作用。 Chrome,Firefox等用%20替換空格並且可行。 – ashishduh 2014-11-06 17:00:14

+0

文件名是「something.example.com This File.pdf」? – iheanyi 2014-11-06 18:28:05

回答

0

例外java.io.FileNotFoundException將被退回。有時錯誤代碼和返回的數據不匹配。

boolean isError = urlConnection.getResponseCode() >= 400; 
InputStream inputStream = = isError ? urlConnection.getErrorStream() : urlConnection.getInputStream(); 

如果你連接到一個非標準的端口,您可以通過添加這些頭您的要求修復它:

您可以使用以下檢查這個(並獲得返回的任何數據)
urlConnection.setRequestProperty("User-Agent","Mozilla/5.0 (compatible) "); 
urlConnection.setRequestProperty("Accept","*/*"); 
+0

'urlConnection.getInputStream()'拋出異常' – ashishduh 2014-11-06 17:58:13

0

您的UrlEncoder嘗試錯誤。這裏的Javadoc: http://docs.oracle.com/javase/6/docs/api/java/net/URLEncoder.html

java.io.FileNotFoundException: http://www.example.com/directory/something.example.com這File.pdf 空間沒有編碼,因此它不符合您的文件。

java.io.FileNotFoundException: http://www.example.com/directory/something.example.com%20This%20File.pdf %20是有效的url符號,所以這也與您的文件不匹配。未發現協議::

java.net.MalformedURLException HTTP%3A%2F%2Fwww.example.com%目錄%2Fsomething.example.com +後.COM這+ File.pdf 斜線不正確編碼。導致編碼錯誤的網址。

  1. 使用UrlEncoder,您應該對文件名進行編碼並將其與URL連接起來,而不是編碼整個url字符串並使用UTF-8編碼。其他任何東西(不是UTF-8)都不能保證工作。
  2. 如果手動編碼空間不會通過UrlEncoder傳遞它們。
+0

僅通過URLEncoder傳遞文件名會產生'http://www.example。com/directory/something.example.com + This + File.pdf'服務器無法識別爲文件,因此我下載了一個錯誤頁面。如果我無法控制服務器,我該如何解決這個問題? – ashishduh 2014-11-07 16:48:24

+0

這很奇怪。之前,當您傳入錯誤的文件名時,您會收到文件未找到異常。什麼改變了? – iheanyi 2014-11-07 16:59:19

+0

在我的3個例子中,我從未傳入'+'分隔的文件名,但情況#3中編碼錯誤的URL除外。 – ashishduh 2014-11-07 20:25:08