我的目标是将远程服务器中的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());
}
}
它是一个特定的文件吗?或者任何70个文件?如果它是一个特定的文件,当您尝试在浏览器中加载该文件时会发生什么? –
Paul,它不是一个特定的文件(该文件在浏览器中正常加载)。它在阅读70个文件后发生。 –
对不起保罗,刚才发现,这个错误是只触发了总共200个中的8个特定文件。必须找到这8个文件有什么问题 –