尝试下载映像时出现Jsoup 403错误

时间:2018-09-04 08:00:44

标签: java jsoup

每当我尝试下载图像时,我总是收到403错误。即使我可以在浏览器中访问图像。我怎样才能解决这个问题?到目前为止,这是我的代码:

private void setImage(Company company) throws IOException {
    Elements companyImageElement = companyDocument.select("div[class^=image] > img");
    URL originImageUrl = new URL(companyImageElement.attr("abs:src"));
    URLConnection urlConnection = originImageUrl.openConnection();
    urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
    String destinationPathName = "classpath://resources/image.jpg";
    File destinationImageFile = new File(destinationPathName);
    copyUrlToFile(originImageUrl, destinationImageFile);
}

public static void copyUrlToFile(URL url, File file) throws IOException {
    FileUtils.copyURLToFile(url, file);
}

0 个答案:

没有答案