我的目標是將遠程服務器中的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個文件有什麼問題 –