gpt4 book ai didi

从远程服务器下载文件到Android手机时出现java.io.eofException

转载 作者:行者123 更新时间:2023-12-02 04:37:03 26 4
gpt4 key购买 nike

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

}

最佳答案

也许该文件已损坏?

收到该错误后,您是否尝试再次执行该操作?

例如在抛出异常之后:

 catch (Exception e) {
throw new Exception("Error downloading pathway overview images :" + e.getMessage());
}

捕获它并调用该方法 protected void doDownloadPathwayImageFiles(final String fileName, Final int TotalNumberOfFiles, Final int downloadFiles) 在代码中再次抛出异常(即:第三次忽略文件并尝试下载下一步)。

关于从远程服务器下载文件到Android手机时出现java.io.eofException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30619703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com