Java
Chilkat2 Java
Example: Http.ResumeDownload method
Demonstrates theResumeDownload method.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.FileAccess;
import com.chilkatsoft.chilkat2.Http;
public class ChilkatExample {
public static void main(String[] args) {
boolean success = false;
Http http = new Http();
http.setKeepResponseBody(true);
// To demonstrate resuming a download, we've created a file on the chilkatsoft.com
// web server that contains the first 82,379 bytes of the full 279,658 bytes of the hamlet.xml file.
// We'll first download the partial file, and pretend it was a previous incomplete attempt to download the entire file.
String localFilePath = "c:/temp/qa_output/hamlet.xml";
try {
http.download("https://www.chilkatsoft.com/testData/hamlet_partial.xml", localFilePath);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Download the remainder of hamlet.xml.
try {
http.resumeDownload("https://www.chilkatsoft.com/testData/hamlet.xml", localFilePath);
success = true;
} catch (ChilkatException e) {
success = false;
}
int statusCode = http.getLastStatus();
if (!success) {
if (statusCode == 0) {
// Unable to either send the request or get the response.
System.out.println(http.getLastErrorText());
} else {
// We got a response, but the status code was not in the 200s
System.out.println("Response status code: " + statusCode);
// Examine the response body.
System.out.println("Response body:");
System.out.println(http.getLastResponseBody());
}
System.out.println("Download failed.");
} else {
System.out.println("Download success, response status = " + statusCode);
}
// The hamlet.xml file should be 279,658 bytes
FileAccess fac = new FileAccess();
System.out.println("size of hamlet.xml: " + fac.fileSize(localFilePath));
System.out.println("Success.");
}
}