2016-11-26 74 views
-1

我想實現簡單的下載管理我的Android應用程序,我用簡單的Serivce從互聯網上下載文件,當我用這個方法用於創建連接:的Android connection.getInputStream()返回FileNotFoundException異常錯誤

private HttpURLConnection createConnection(String urlStr) throws IOException { 
    URL    url = new URL(urlStr); 
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
    conn.setDoInput(true); 
    conn.setDoOutput(true); 
    conn.setReadTimeout(timeout); 
    conn.setConnectTimeout(timeout); 
    return conn; 
} 

我沒有得到任何異常,此鏈接做工精細:

https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png 

現在我想從連接getInputStream()並分配變量:

in = new BufferedInputStream(connection.getInputStream(),BUFFER_SIZE);

,但我得到這個錯誤:

'java.io.FileNotFoundException' exception. 

我可以得到文件長度罰款從連接,但我不能下載

new Thread(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       prepareDownload(fileUrl, filePath, lastModified); 
       connection = createConnection(fileUrl); 
       fileDownloadStatus = downloadStatus.DOWNLOADING; 
       if (!startNewDownload) { 
        connection.setRequestProperty("Range", "bytes=" + downloadedFile.length() + "-"); 
       } 
       if (onDownloadProcess != null) 
        onDownloadProcess.fileLength(fileLength); 
       in = new BufferedInputStream(connection.getInputStream(), BUFFER_SIZE); 
       long progressLength = 0; 
       if (!startNewDownload) { 
        progressLength += downloadedFile.length(); 
        // append to exist downloadedFile 
        writer = new FileOutputStream(filePath, true); 
       } else { 
        writer = new FileOutputStream(filePath); 
        // save remote last modified data to local 
        lastModified = connection.getHeaderField("Last-Modified"); 
       } 
       try { 
        byte[] buffer = new byte[BUFFER_SIZE]; 
        int count; 
        while (getFileDownloadStatus() == downloadStatus.DOWNLOADING && (count = in.read(buffer)) != -1) { 
         progressLength += count; 
         writer.write(buffer, 0, count); 
         // progress.... 
         if (onDownloadProcess != null) 
          onDownloadProcess.onProgress((int) (progressLength * 100/fileLength)); 
         if (progressLength == fileLength) { 
          progressLength = 0; 
          setFileDownloadStatus(downloadStatus.COMPLETE); 
         } 
        } 
       ... 
    }).start(); 

回答

0

首先,我看到你使用的HttpConnection爲https網址。 如果請求失敗(無法找到指定的文件),則會收到FileNotFound。這可能是因爲這一點。

下面是我在我的應用程序使用WebRequest類的一個片段,它爲所有HTTPS下載

 URL url = new URL(request); 

     con = (HttpsURLConnection)url.openConnection(); 
     con.setDoOutput(true); 
     con.setInstanceFollowRedirects(false); 
     con.setRequestProperty("charset", "utf-8"); 
     con.setUseCaches(false); 

     DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 
     wr.write(postData); 
     wr.flush(); 
     wr.close(); 

     // those are from my class to return the server response 
     result.setResultCode(con.getResponseCode()); 
     result.setResultMessage(con.getResponseMessage()); 

     InputStream stream = con.getInputStream(); 
     InputStreamReader reader = new InputStreamReader(stream); 
     BufferedReader br = new BufferedReader(reader); 

這是HTML請求(讀,直到文本的結尾)的作品不錯,但我認爲你可以請使用我在con上設置的屬性來進行圖片下載。

+0

問題不解決,我得到這個錯誤爲每個http,https網址 –

相關問題