0

我的目标是将远程服务器中的 200 个 .jpg 文件下载到我的 android 手机(运行 jellybeans)。为了做到这一点,我在循环中运行下面的方法,并将不同的文件名分配给文件名参数。它运行良好,直到我下载了 70 个文件。之后我得到一个 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());
                }

        }
4

3 回答 3

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(即:第三次忽略文件并尝试下载下一步)。

于 2015-06-03T13:55:08.557 回答
1

这可能是因为您不断地从文件中读取 1024 个字节,这可能比剩下要读取的要多。

您可能想检查正在下载的文件的内容长度,并且只阅读您需要的内容。

我还会检查响应代码是否为 200(OK)。以及服务器返回的响应的 Content Length 标头:

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

如果小于 1,则不要尝试继续读取文件,因为它无效。Content-Length 是要返回的文件的字面量大小,因此如果您尝试读取此文件,则会收到 End of File 异常,因为响应中没有要读取的数据。

于 2015-06-03T15:39:20.163 回答
0

终于找到了确切的原因。我正在连接到 nginx 服务器(测试服务器)以下载相应的文件。在 nginx 中有这个报告的问题“URI 上的行为不一致,后跟 H 的未编码空格”(http://trac.nginx.org/nginx/ticket/196 )。因此 nginx 将错误的请求错误返回给客户端。最终会在 connection.openStram() 中引发 eof 异常。

由于我正在开发的软件的生产版本不需要连接到 nginx 实例。我只是更改了服务器,不再使用 nginx。但我仍然必须使用 String.replace() 方法用 '%20' 替换 URL 中的空格。

于 2015-06-09T13:46:08.440 回答