我的目标是将远程服务器中的 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());
}
}