4

I'm trying to download images from url but got java.io.IOException: exception. My code is:

    public static void main(String[] args) throws MalformedURLException, IOException {

    File picutreFile = new File("test.jpg");
    FileUtils.copyURLToFile(new java.net.URL("http://paceoil.ca/files/includes/images/images-stories-presentation-october-icon-graphic.jpg"), picutreFile);

}

When run threw :

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://paceoil.ca/files/includes/images/images-stories-presentation-october-icon-graphic.jpg
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at org.apache.commons.io.FileUtils.copyURLToFile(FileUtils.java:1460)
at com.hrant.Test.main(Test.java:14)

This code works for some urls but for this I couldnt understand why not. Advance many Thanks.

Hrant Vardanyan
  • 265
  • 1
  • 4
  • 11
  • 2
    403 means forbidden. so you don't have permission to access the URL – user3662273 Sep 02 '14 at 18:49
  • I know that it means forbidden......but I have permission to access because it works on browser.. – Hrant Vardanyan Sep 02 '14 at 18:52
  • Do you have to log into the site you are trying to download from? Can you right click and save the image in browser? Also you spelled picture wrong in `File picutreFile`, you might want to change that to prevent future bugs. – turbo Sep 02 '14 at 18:57

3 Answers3

10

try setting user agent property on your url, for example, see if it helps:

File picutreFile = new File("src/test.jpg");
         URL url=new URL("http://paceoil.ca/files/includes/images/images-stories-presentation-october-icon-graphic.jpg");
         URLConnection conn = url.openConnection();
         conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0");
         conn.connect();
            FileUtils.copyInputStreamToFile(conn.getInputStream(), picutreFile);
user3487063
  • 3,672
  • 1
  • 17
  • 24
0

Just a suggestion:

When this only happen to this URL (or Domain), it may be possible, that the http-server implementation on the other end is checking some HTTP-Headers before processing the request., And you do not send such informations.

The result may be, that the server will response with 403.

Maybe: Forbidden to unknown Client (e.g User-Agent)

Maybe this Link will help to add headers to your request:
https://stackoverflow.com/a/15555952/3887073

Community
  • 1
  • 1
Ben
  • 3,378
  • 30
  • 46
0

this server maybe dont promise the some request that has invalid HTTP request . you can send your HTTP header file with you request and server will promise you to download this image if its need the some HTTP header information . something like this :

URL myURL = new URL(serviceURL);
HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
String userCredent = "username:password";
myURLConnection.setRequestProperty ("property", value);
myURLConnection.setRequestMethod("POST");
amir
  • 2,443
  • 3
  • 22
  • 49