i have used windows 7 os, chrome/40.0.2214.93
I try to fetch image from url using java
My java code is
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedImage img1 = null;
BufferedImage img2 = null;
InputStream inputstream=null;
URLConnection urlcon=null;
try {
URL url1 = new URL("http://rosettacode.org/mw/images/3/3c/Lenna50.jpg");
URL url2 = new URL("http://rosettacode.org/mw/images/b/b6/Lenna100.jpg");
urlcon=url1.openConnection();
urlcon.addRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36");
img1 = ImageIO.read(url1.openStream());
img2 = ImageIO.read(url2.openStream());
} catch (IOException e) {
e.printStackTrace();
}
int width1 = img1.getWidth(null);
int width2 = img2.getWidth(null);
int height1 = img1.getHeight(null);
int height2 = img2.getHeight(null);
if ((width1 != width2) || (height1 != height2)) {
System.err.println("Error: Images dimensions mismatch");
System.exit(1);
}
long diff = 0;
for (int y = 0; y < height1; y++) {
for (int x = 0; x < width1; x++) {
int rgb1 = img1.getRGB(x, y);
int rgb2 = img2.getRGB(x, y);
int r1 = (rgb1 >> 16) & 0xff;
int g1 = (rgb1 >> 8) & 0xff;
int b1 = (rgb1 ) & 0xff;
int r2 = (rgb2 >> 16) & 0xff;
int g2 = (rgb2 >> 8) & 0xff;
int b2 = (rgb2 ) & 0xff;
diff += Math.abs(r1 - r2);
diff += Math.abs(g1 - g2);
diff += Math.abs(b1 - b2);
}
}
double n = width1 * height1 * 3;
double p = diff / n / 255.0;
System.out.println("diff percent: " + (p * 100.0));
}
}
The error when i run the application
java.io.IOException: Server returned HTTP response code: 403 for URL: http://rosettacode.org/mw/images/3/3c/Lenna50.jpg
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at ImgDiffPercent.main(ImgDiffPercent.java:32)
Exception in thread "main" java.lang.NullPointerException
at ImgDiffPercent.main(ImgDiffPercent.java:37)
Already i tried guidelines in stack flow related to that but still the problem not solved. Help me to solve it.
Thanks...
403 Forbiddenmeans that your client is not allowed to acces the URL. What does the server require?