2017-10-02 18 views
1

他の画像は動作しますが、明らかにこれはありません。Java | URLから画像を保存する| java.io.IOException:403のURLは

https://www.kingjamesbibleonline.org/Inspirational-Images/large/Isaiah_55-6.jpg

コード:

エラーメッセージ
public static void imageFromURL(URL url, String saveAs) throws IOException { 
    HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection(); 
    httpURLCon.addRequestProperty("User-Agent", "Mozilla/4.76"); 
    BufferedImage c = ImageIO.read(url.openStream()); 
    File outputFile = new File("C:.../resources/" + saveAs); 
    ImageIO.write(c, "jpg", outputFile); 
} 

HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); 
httpcon.addRequestProperty("User-Agent", "Mozilla/4.76"); 

java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.kingjamesbibleonline.org/Inspirational-Images/large/Isaiah_55-6.jpg 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840) 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441) 
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) 
    at java.net.URL.openStream(URL.java:1045) 
    at MainActivity.imageFromURL(MainActivity.java:41) 
    at MainActivity.main(MainActivity.java:49) 

持ってのような他の解決さを試してみました

uc = url.openConnection(); 
uc.addRequestProperty("User-Agent", 
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); 

URLConnection conn = url.openConnection(); 
conn.setRequestProperty("User-Agent", "Mozilla/5.0"); 

答えて

0

あなたは既に画像URLへの接続をオープンし、httpURLConでそれを保存しました。それはあなたがユーザーエージェントを設定している一つですので

は、リユース、その接続、:

HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection(); 
httpURLCon.addRequestProperty("User-Agent", "Mozilla/4.76"); 
BufferedImage c = ImageIO.read(httpURLCon.getInputStream()); 
+0

のgetInputStreamにopenStream()を変更する()多くのことを助けるように見えました。 –

関連する問題