Überprüfen Sie die Größe der teilweise heruntergeladenen Datei < /li>
Setzen Sie den richtigen Bereich < /code> in der HTTP -Anforderung < /li>
Anhang auf die vorhandene Datei. Code, den ich benutze, was die Datei jedes Mal von Anfang an herunterlädt :
Code: Select all
public static void downloadFileWithResume(String fileURL, String destination) throws IOException {
File file = new File(destination);
long existingFileSize = file.exists() ? file.length() : 0;
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
if (existingFileSize > 0) {
httpConn.setRequestProperty("Range", "bytes=" + existingFileSize + "-");
System.out.println("Resuming download from byte " + existingFileSize);
}
int responseCode = httpConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_PARTIAL || responseCode == HttpURLConnection.HTTP_OK) {
try (InputStream inputStream = httpConn.getInputStream();
RandomAccessFile outputFile = new RandomAccessFile(destination, "rw")) {
outputFile.seek(existingFileSize);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputFile.write(buffer, 0, bytesRead);
}
}
} else {
throw new IOException("Server replied with HTTP code: " + responseCode);
}
httpConn.disconnect();
}
}
package com.example.archivibility.afardi.archivibilty;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.Executors;
import java.util.Collections;
import java.io.IOException;
public class DataTransfer {
// خصوصیات کلاس
private ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
private List dataList = Collections.synchronizedList(new ArrayList());
private ArchivabilityDataManager archivabilityDataManager;
// روش برای برنامهریزی فعالیت insertData هر 10 ثانیه
public void insertDataService() {
service.scheduleWithFixedDelay(this::insertData, 0, 10, TimeUnit.SECONDS);
}
// روش برای ذخیره دادهها در فایل
public void insertData() {
// اضافه کردن افراد به لیست
dataList.add(new Person("amir"));
dataList.add(new Person("leila"));
dataList.add(new Person("hossein"));
dataList.add(new Person("bita"));
dataList.add(new Person("milad"));
dataList.add(new Person("reza"));
dataList.add(new Person("amir"));
// حلقه برای ذخیره هر شخص در فایل
for (Person person : dataList) {
try {
archivabilityDataManager = new ArchivabilityDataManager("src/main/java/com/example/archivibility/afardi/archivibilty/", 11);
archivabilityDataManager.saveData("data", person.getName());
} catch (IOException e) {
e.printStackTrace();
}
}
}
// روش برای خاموش کردن خدمات
public void shutDown() {
service.shutdown();
}
}