在网页中下载图片时出现错误403

时间:2019-01-13 14:52:13

标签: java url connection user-agent

我正在寻找一个程序来下载网页的所有图像,但是当我启动该程序时,出现了403错误。

在Internet上阅读时,我试图添加一个用户代理,但这并不能解决问题。

URL url2 = new URL(m.group().substring(2));
URLConnection uc = url2.openConnection();
uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64;rv:52.0) Gecko/20100101 Firefox/52.0");
uc.connect();
Files.copy(uc.getInputStream(),  Paths.get("C:\\Users\\Oromis\\Downloads\\Textures\\New\\" + ++i + ".png"));

我希望将图像保存在目录中,但是实际上,我遇到了这个错误:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://d1u5p3l4wpay3k.cloudfront.net/minecraft_fr_gamepedia/thumb/5/5c/B%C3%BBche_de_ch%C3%AAne_TU.png/160px-B%C3%BBche_de_ch%C3%AAne_TU.png?version=54ead9ac1cdc9f7287b4c4575183abea 2x
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at Test.main(Test.java:31)

0 个答案:

没有答案