sophe
asked on
Security exception on ImageIcon.getImage()
Hi,
I'm writing an applet which uses images downloaded from google images. Generally this works fine but in some cases when the image isn't available i get a security exception which is not caught by the following code:
BufferedImage bufferedImage = null;
try{
//convert string to url as Imageicon gets url address.
URL url = new URL(imageLink);
//ensures that all pixels have been loaded before returning
Image img = new ImageIcon(url).getImage();
bufferedImage = getBufferedImageFromImage( img);
} catch(Exception e) {
e.printStackTrace();
}
return bufferedImage;
The error stack generated is:
Uncaught error fetching image:
java.lang.SecurityExceptio n
at java.lang.SecurityManager. checkPermi ssion(Unkn own Source)
at java.lang.SecurityManager. checkConne ct(Unknown Source)
at sun.awt.image.URLImageSour ce.checkSe curity(Unk nown Source)
at sun.awt.image.ImageReprese ntation.im ageComplet e(Unknown Source)
at sun.awt.image.InputStreamI mageSource .errorCons umer(Unkno wn Source)
at sun.awt.image.InputStreamI mageSource .setDecode r(Unknown Source)
at sun.awt.image.InputStreamI mageSource .doFetch(U nknown Source)
at sun.awt.image.ImageFetcher .fetchloop (Unknown Source)
at sun.awt.image.ImageFetcher .run(Unkno wn Source)
Any comments would be appreciated.
Sophe
I'm writing an applet which uses images downloaded from google images. Generally this works fine but in some cases when the image isn't available i get a security exception which is not caught by the following code:
BufferedImage bufferedImage = null;
try{
//convert string to url as Imageicon gets url address.
URL url = new URL(imageLink);
//ensures that all pixels have been loaded before returning
Image img = new ImageIcon(url).getImage();
bufferedImage = getBufferedImageFromImage(
} catch(Exception e) {
e.printStackTrace();
}
return bufferedImage;
The error stack generated is:
Uncaught error fetching image:
java.lang.SecurityExceptio
at java.lang.SecurityManager.
at java.lang.SecurityManager.
at sun.awt.image.URLImageSour
at sun.awt.image.ImageReprese
at sun.awt.image.InputStreamI
at sun.awt.image.InputStreamI
at sun.awt.image.InputStreamI
at sun.awt.image.ImageFetcher
at sun.awt.image.ImageFetcher
Any comments would be appreciated.
Sophe
ASKER
sorry i don't understand your question.
it is simply using a url to a file found via google images.
here is an example url that causes this problem: http://6a.typepad.com/images/missing.gif
sophe
it is simply using a url to a file found via google images.
here is an example url that causes this problem: http://6a.typepad.com/images/missing.gif
sophe
Copy that image to your project and access it as local resource.
In general a random file is not accessible by a JAVA client because of security restrictions.
You can't access files local to the client without access being granted, the same for
files on some random system, otherwise the former situation could be 'circumvented'.
So, copy the image to the directory of one of your class files and load it as in this snippet:
/**
* Safely get the path to a resource file via Class info.
* <p>If the file is missing, assuming that the resource was an image file,<BR>
* the packaged <b>MISSING.gif</b> icon is substituted,
* using the class info <b>packageClass</b> retrieved elsewhere.</p>
* @param classinfo a Class loaded from the root of relative pathname.
* @param filename the relative pathname to the resource file.
* @return an URL to the resulting resource.
*/
private static URL findResource(Class classInfo, String filename)
{
URL resourceURL;
if(classInfo == null)
{
fatal("BAD CLASS INFO");
}
if((resourceURL = classInfo.getResource(file name)) == null)
{
error("RESOURCE FILE '" + filename + "' MISSING.");
if((resourceURL
= packageClass.getResource(" res/MISSIN G.gif")) == null)
{
fatal("SUBSTITUTE IMAGE 'MISSING' NOT FOUND.");
}
}
return(resourceURL);
}
// And deploying this in my program elsewhere (assume JFrame mainFrame; ):
/**
* Set the default icon on the program main JFrame.
* <p>Can be used to reset the icon after calls with an argument.<BR>
* The default icon should be <b>res/<program>.gif</b>.< /p>
*/
public static void setMainFrameIcon( )
{
mainFrame.setIconImage
(
Toolkit.getDefaultToolkit( ).getImage (findResou rce(this.g etClass( ), "res/Application.gif"))
);
}
;JOOP!
In general a random file is not accessible by a JAVA client because of security restrictions.
You can't access files local to the client without access being granted, the same for
files on some random system, otherwise the former situation could be 'circumvented'.
So, copy the image to the directory of one of your class files and load it as in this snippet:
/**
* Safely get the path to a resource file via Class info.
* <p>If the file is missing, assuming that the resource was an image file,<BR>
* the packaged <b>MISSING.gif</b> icon is substituted,
* using the class info <b>packageClass</b> retrieved elsewhere.</p>
* @param classinfo a Class loaded from the root of relative pathname.
* @param filename the relative pathname to the resource file.
* @return an URL to the resulting resource.
*/
private static URL findResource(Class classInfo, String filename)
{
URL resourceURL;
if(classInfo == null)
{
fatal("BAD CLASS INFO");
}
if((resourceURL = classInfo.getResource(file
{
error("RESOURCE FILE '" + filename + "' MISSING.");
if((resourceURL
= packageClass.getResource("
{
fatal("SUBSTITUTE IMAGE 'MISSING' NOT FOUND.");
}
}
return(resourceURL);
}
// And deploying this in my program elsewhere (assume JFrame mainFrame; ):
/**
* Set the default icon on the program main JFrame.
* <p>Can be used to reset the icon after calls with an argument.<BR>
* The default icon should be <b>res/<program>.gif</b>.<
*/
public static void setMainFrameIcon( )
{
mainFrame.setIconImage
(
Toolkit.getDefaultToolkit(
);
}
;JOOP!
ASKER
The url is found dynamically by searching google images for a keyword entered by the user. This is done with the following code:
/**
*@param keyword is the object to be searched in google
*/
public String makeURLCon (String keyword) {
String source= null;
try{
//search Google images for the keyword
URL url = new
URL("http://images.google.co.uk/images?svnum=10&hl=en&q="+keyword+"&btnG=Sear ch");
URLConnection connection = url.openConnection();
connection.setDoInput(true );
//Sets the general request property
connection.setRequestPrope rty("User- agent","Mo zilla/4.0" );
InputStream inputStrm = connection.getInputStream( );
source = readSource(inputStrm);
}catch (Exception e) {
e.printStackTrace();
}
return source;
}
Generally if the applet sees the image can't be downloaded it moves on to the next url in the source. But this doesn't happen when a SecurityException is thrown for some reason.
So this means that i can't save each image manually.
/**
*@param keyword is the object to be searched in google
*/
public String makeURLCon (String keyword) {
String source= null;
try{
//search Google images for the keyword
URL url = new
URL("http://images.google.co.uk/images?svnum=10&hl=en&q="+keyword+"&btnG=Sear
URLConnection connection = url.openConnection();
connection.setDoInput(true
//Sets the general request property
connection.setRequestPrope
InputStream inputStrm = connection.getInputStream(
source = readSource(inputStrm);
}catch (Exception e) {
e.printStackTrace();
}
return source;
}
Generally if the applet sees the image can't be downloaded it moves on to the next url in the source. But this doesn't happen when a SecurityException is thrown for some reason.
So this means that i can't save each image manually.
If you are sure about that, simply catch it and go on searching:
try
{
// convert string to url as Imageicon gets url address.
URL url = new URL(imageLink);
// ensures that all pixels have been loaded before returning
Image img = new ImageIcon(url).getImage();
bufferedImage = getBufferedImageFromImage( img);
}
catch(Exception e) { /* IGNORE EXCEPTION */ }
return bufferedImage;
// See the corrected code, and heed my style.
;JOOP!
try
{
// convert string to url as Imageicon gets url address.
URL url = new URL(imageLink);
// ensures that all pixels have been loaded before returning
Image img = new ImageIcon(url).getImage();
bufferedImage = getBufferedImageFromImage(
}
catch(Exception e) { /* IGNORE EXCEPTION */ }
return bufferedImage;
// See the corrected code, and heed my style.
;JOOP!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Generally you only have access to the server.
;JOOP!