Я пытаюсь написать функцию, которая загружает файл по определенному URL.Функция создает поврежденный файл, если я не сделаю буфер массивом размером 1 (как в коде ниже).
Тернарный оператор над инициализацией буфера (который я планирую использовать) вместе с жестко запрограммированными целочисленными значениями, отличными от 1, создаст поврежденный файл.
Примечание: MAX_BUFFER_SIZE - это определенная константакак 8192 (2 ^ 13) в моем коде.
public static void downloadFile(String webPath, String localDir, String fileName) {
try {
File localFile;
FileOutputStream writableLocalFile;
InputStream stream;
url = new URL(webPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int size = connection.getContentLength(); //File size in bytes
int read = 0; //Bytes read
localFile = new File(localDir);
//Ensure that directory exists, otherwise create it.
if (!localFile.exists())
localFile.mkdirs();
//Ensure that file exists, otherwise create it.
//Note that if we define the file path as we do below initially and call mkdirs() it will create a folder with the file name (I.e. test.exe). There may be a better alternative, revisit later.
localFile = new File(localDir + fileName);
if (!localFile.exists())
localFile.createNewFile();
writableLocalFile = new FileOutputStream(localFile);
stream = connection.getInputStream();
byte[] buffer;
int remaining;
while (read != size) {
remaining = size - read; //Bytes still to be read
//remaining > MAX_BUFFER_SIZE ? MAX_BUFFER_SIZE : remaining
buffer = new byte[1]; //Adjust buffer size according to remaining data (to be read).
read += stream.read(buffer); //Read buffer-size amount of bytes from the stream.
writableLocalFile.write(buffer, 0, buffer.length); //Args: Bytes to read, offset, number of bytes
}
System.out.println("Read " + read + " bytes.");
writableLocalFile.close();
stream.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
Причина, по которой я написал это таким образом, заключается в том, что я могу предоставить пользователю индикатор выполнения в реальном времени во время загрузки.Я удалил его из кода, чтобы уменьшить беспорядок.