java.io.eofexception 从远程服务器下载文件到 android phone
java.io.eofexception when downloading files from remote server to android phone
我的 objective 是将远程服务器中的 200 个 .jpg 文件下载到我的 android phone(运行 软糖)。为了做到这一点,我在一个循环中使用 运行 下面的方法,将不同的文件名分配给文件名参数。在我下载 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());
}
}
也许该文件已损坏?
您是否在收到该错误后尝试再次进行该操作?
例如在你抛出异常之后:
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 次忽略文件并尝试下一步下载)。
这可能是因为您一直在从文件中读取 1024 字节,这可能比剩余的要多。
您需要检查正在下载的文件的内容长度,并且只阅读您需要的内容。
我还会检查响应代码是否为 200(确定)。以及服务器返回的响应的Content Length头:
long contentLength = Long.parseLong(connection.getHeaderField("Content-Length"));
如果小于 1,请勿尝试继续读取文件,因为它将无效。 Content-Length 是返回文件的字面大小,因此如果您尝试读取此文件,您将遇到文件结束异常,因为响应中没有可读取的数据。
终于找到确切原因了。我正在连接到 nginx 服务器(测试服务器)以下载相应的文件。在 nginx 中有这个报告的问题 "Inconsistent behavior on uri's with unencoded spaces followed by H" (http://trac.nginx.org/nginx/ticket/196)。因此nginx returns bad request错误返回给客户端。最终在 connection.openStram().
中抛出一个 eof 异常
因为我正在使用的软件的生产版本不需要连接到 nginx 实例。我只是换了服务器,不再使用nginx了。但我仍然必须使用 String.replace() 方法将 URL 中的空格替换为 '%20'。
我的 objective 是将远程服务器中的 200 个 .jpg 文件下载到我的 android phone(运行 软糖)。为了做到这一点,我在一个循环中使用 运行 下面的方法,将不同的文件名分配给文件名参数。在我下载 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());
}
}
也许该文件已损坏?
您是否在收到该错误后尝试再次进行该操作?
例如在你抛出异常之后:
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 次忽略文件并尝试下一步下载)。
这可能是因为您一直在从文件中读取 1024 字节,这可能比剩余的要多。
您需要检查正在下载的文件的内容长度,并且只阅读您需要的内容。
我还会检查响应代码是否为 200(确定)。以及服务器返回的响应的Content Length头:
long contentLength = Long.parseLong(connection.getHeaderField("Content-Length"));
如果小于 1,请勿尝试继续读取文件,因为它将无效。 Content-Length 是返回文件的字面大小,因此如果您尝试读取此文件,您将遇到文件结束异常,因为响应中没有可读取的数据。
终于找到确切原因了。我正在连接到 nginx 服务器(测试服务器)以下载相应的文件。在 nginx 中有这个报告的问题 "Inconsistent behavior on uri's with unencoded spaces followed by H" (http://trac.nginx.org/nginx/ticket/196)。因此nginx returns bad request错误返回给客户端。最终在 connection.openStram().
中抛出一个 eof 异常因为我正在使用的软件的生产版本不需要连接到 nginx 实例。我只是换了服务器,不再使用nginx了。但我仍然必须使用 String.replace() 方法将 URL 中的空格替换为 '%20'。