2015-06-03 41 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.我刚刚检查过它。这是否意味着它自身的图像文件存在问题? –

相关问题