Em đang sử dụng Jsoup để download ảnh từ link sau: http://www.allitebooks.in/pcs-just-the-steps-for-dummies-2nd-edition/ nhưng khi em sử dụng đoạn code sau để download ảnh về local thì gặp lỗi 406
package test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class TestGetCategory {
private static String IMAGE_DESTINATION_FOLDER = "C:/images";
public static void main(String[] args) throws IOException {
// replace it with your URL
String strURL = "http://www.allitebooks.in/pcs-just-the-steps-for-dummies-2nd-edition/";
// connect to the website and get the document
Document document = Jsoup.connect(strURL).userAgent("Mozilla/5.0").timeout(10 * 1000).get();
// select all img tags
Elements imageElements = document.select("div.td-post-content > p:eq(0) > img");
// iterate over each image
for (Element imageElement : imageElements) {
// make sure to get the absolute URL using abs: prefix
String strImageURL = imageElement.attr("abs:src");
// download image one by one
downloadImage(strImageURL);
}
}
private static void downloadImage(String strImageURL) {
// get file name from image path
String strImageName = strImageURL.substring(strImageURL.lastIndexOf("/") + 1);
System.out.println("Saving: " + strImageName + ", from: " + strImageURL);
try {
// open the stream from URL
URL urlImage = new URL(strImageURL);
InputStream in = urlImage.openStream();
byte[] buffer = new byte[4096];
int n = -1;
OutputStream os = new FileOutputStream(IMAGE_DESTINATION_FOLDER + "/" + strImageName);
// write bytes to the output stream
while ((n = in.read(buffer)) != -1) {
os.write(buffer, 0, n);
}
// close the stream
os.close();
System.out.println("Image saved");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Exception in thread "main" org.jsoup.HttpStatusException: HTTP error fetching URL. Status=406, URL=http://www.allitebooks.in/pcs-just-the-steps-for-dummies-2nd-edition/
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:776)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:722)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:306)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:295)
at test.TestGetCategory.main(TestGetCategory.java:24)
Em có tham khảo một bài trên mạng nhưng em đọc không hiểu cho lắm (https://stackoverflow.com/questions/51777354/jsoup-connection-http-error-406)
Mong được anh/chị giúp đỡ. Em cảm ơn anh/chị nhiều ạ !