我的目标是将远程服务器中的200个.jpg文件下载到我的Android手机(运行软糖)。为了做到这一点,我在一个循环中运行下面的方法,不同的文件名分配给filename参数。它运行良好,直到我下载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());
}
}
答案 0 :(得分: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
(即:第三次忽略文件并尝试下载)。
答案 1 :(得分:1)
这可能是因为你经常从文件中读取1024个字节,这可能比剩下的还要多。
您需要查看正在下载的文件的内容长度,并且只读取您需要的内容。
我还会检查响应代码是否为200(OK)。以及服务器返回的响应的Content Length标头:
long contentLength = Long.parseLong(connection.getHeaderField("Content-Length"));
如果是<
请勿尝试继续读取文件。 1因为它不会有效。 Content-Length是要返回的文件的文字大小,因此如果您尝试读取此文件,则会收到“文件结束”异常,因为响应中没有要读取的数据。
答案 2 :(得分:0)
最后设法找出确切的原因。我正在连接到nginx服务器(测试服务器)以获取下载的相应文件。这是一个报道的问题" uri的行为不一致,其中有未编码的空格,其次是H" (http://trac.nginx.org/nginx/ticket/196)在nginx中。因此,nginx将错误的请求错误返回给客户端。最终会在connection.openStram()中抛出一个eof异常。
由于我正在处理的软件的生产版本并不需要连接到nginx实例。我刚刚更改了服务器,不再使用nginx了。但我仍然需要用'%20'替换URL中的空格。使用String.replace()方法。