и 2-how
Код: Выделить всё
public class ResumableDownloader {
public static void download(String fileURL, String savePath) throws IOException {
File file = new File(savePath);
long existingFileSize = file.exists() ? file.length() : 0;
URL url = new URL(fileURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// Set Range header to resume download
conn.setRequestProperty("Range", "bytes=" + existingFileSize + "-");
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_PARTIAL || responseCode == HttpURLConnection.HTTP_OK) {
try (InputStream in = conn.getInputStream();
RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
System.out.println(existingFileSize);
raf.seek(existingFileSize); // Move to the end of existing file
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
raf.write(buffer, 0, bytesRead);
}
System.out.println("Download complete.");
}
} else {
System.out.println("Server does not support partial content. Response code: " + responseCode);
}
conn.disconnect();
}
public static void main(String[] args) throws IOException {
String fileURL = "https://pdn.sharezilla.ir/com.rar";
String savePath = "largefile.rar";
download(fileURL, savePath);
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... downloader