2015-06-03 39 views
0

我的目標是將遠程服務器中的200個.jpg文件下載到我的android手機(運行軟糖)。爲了做到這一點,我在循環中運行下面的方法,使用不同的文件名分配給filename參數。它運行良好,直到我下載70個文件。之後,我得到一個java.io.eofexception。你能幫忙嗎?將文件從遠程服務器下載到Android手機時的java.io.eofexception

protected void doDownloadPathwayImageFiles(final String fileName, final int totalNumberOfFiles, final int downloadedFiles) throws Exception { 


       File root = android.os.Environment.getExternalStorageDirectory(); 
       File dir = new File(root.getAbsolutePath() + "/" + UtilConstants.PATHWAY_IMAGE_FOLDER + "/"); 
       if (dir.exists() == false) { 
        dir.mkdirs(); 
       } 

       try { 
        URL url = new URL(UtilConstants.PATHWAY_FILE_LOCATION + fileName + UtilConstants.IMAGE_FILE_EXTENSION); 
        Log.i("FILE_NAME", "File name is " + fileName); 
        Log.i("FILE_URLLINK", "File URL is " + url); 

        HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
        //conn.setReadTimeout(10000 /* milliseconds */); 
        //conn.setConnectTimeout(15000 /* milliseconds */); 
        conn.setRequestMethod("GET"); 
        conn.setDoInput(true); 
        conn.setRequestProperty("Connection", "close"); 
        // Starts the query 
        conn.connect(); 
        int response = conn.getResponseCode(); 
        Log.i("NETWORK_RESPONSE", "The response is___: " + response); 

        // download the file 
        InputStream urlStream = conn.getInputStream(); 
        InputStream input = new BufferedInputStream(urlStream); 

        OutputStream output = new FileOutputStream(dir + "/" + fileName + UtilConstants.IMAGE_FILE_EXTENSION); 

        //write the file to local directory 
        byte data[] = new byte[1024]; 
        long total = 0; 
        int count; 
        while ((count = input.read(data)) != -1) { 
         total += count; 
         output.write(data, 0, count); 
        } 

        output.flush(); 
        output.close(); 
        input.close(); 
        urlStream.close(); 
        conn.disconnect(); 



        Log.i("Files", "Downloaded " + downloadedFiles); 
        Log.i("Files", "Total " + totalNumberOfFiles); 

        double progressCount = ((double) downloadedFiles/(double) totalNumberOfFiles) * 100; 

        Log.i("percentage", "Progress ++++++++++ " + progressCount); 

        progressBar.setProgress((int) Math.round(progressCount)); 

       }catch (MalformedURLException e) { 
        throw new MalformedURLException("Error downloading pathway overview images :" + e.getMessage()); 
       } catch (IOException e){ 
        throw new IOException("Error downloading pathway overview images :" + e.getMessage()); 
       } catch (Exception e) { 
        throw new Exception("Error downloading pathway overview images :" + e.getMessage()); 
       } 

     } 
+0

它是一個特定的文件嗎?或者任何70個文件?如果它是一個特定的文件,當您嘗試在瀏覽器中加載該文件時會發生什麼? –

+0

Paul,它不是一個特定的文件(該文件在瀏覽器中正常加載)。它在閱讀70個文件後發生。 –

+0

對不起保羅,剛纔發現,這個錯誤是隻觸發了總共200箇中的8個特定文件。必須找到這8個文件有什麼問題 –

回答

0

終於找到了確切的原因。我連接到一個nginx服務器(測試服務器),以獲得相應的文件下載。這個報告的問題是「在uri的非一致編碼空間後跟H的行爲不一致」(http://trac.nginx.org/nginx/ticket/196)在nginx中。因此,nginx將錯誤的請求錯誤返回給客戶端。最終在connection.openStram()中拋出一個eof異常。

由於我正在使用的軟件的生產版本不需要連接到nginx實例。我只是改變了服務器,不再使用nginx。但是我仍然必須使用String.replace()方法替換URL中的空格,並使用'%20'。

1

也許那個文件損壞了?

您是否在嘗試接受該錯誤後再次進行該操作?

例如你扔異常後:

catch (Exception e) { 
       throw new Exception("Error downloading pathway overview images :" + e.getMessage()); 
      } 

抓住它,並在你的代碼再次調用該方法protected void doDownloadPathwayImageFiles(final String fileName, final int totalNumberOfFiles, final int downloadedFiles) throws Exception(即:第3次就忽略文件,並嘗試下下載)。

+0

帕維爾,我試過了重試並忽略策略,已經解釋過,而且有200個文件沒有下載。感謝您的高舉。我仍然想知道爲什麼這8個文件得到EOF異常。這些文件在瀏覽器中正常打開,因此看起來沒有損壞。試圖找出現在 –

+0

你可以在HttpUrlConnection上調用getContentLength()並查看它返回的內容嗎?如果它返回0長度,這可能是一個問題。 –

+0

正好它返回的長度爲0。並且響應代碼也不是200,因爲它在成功的情況下。這是否意味着圖像文件本身存在問題? –

1

這可能是因爲你總是從文件中讀取1024個字節,這可能比剩下的要多。

你會想檢查你正在下載的文件的內容長度,只讀你需要什麼。

我也會檢查響應碼是200(OK)。而由服務器返回的響應的內容長度標題:

long contentLength = Long.parseLong(connection.getHeaderField("Content-Length")); 

不要試圖繼續讀取該文件,如果這是< 1,因爲這將是無效的。 Content-Length是要返回的文件的字面大小,因此如果您嘗試讀取該文件,您將得到文件結束(End of File)異常,因爲在響應中沒有要讀取的數據。

+0

Paul,EOF異常觸發conn.getInputStream();即在讀取字節之前。 –

+0

增加了幾個建議。 –

+0

對,保羅。內容長度爲0.我剛剛檢查過它。這是否意味着它自身的圖像文件存在問題? –

相關問題