Solved

Applet accessig image URL on same domain

Posted on 2006-07-03
21
424 Views
Last Modified: 2012-05-07
I have an applet running at www.zzz.co.uk/email/applet.htm.

The applet attempts to load an image from the same site into a JTextPane via the following:

        ImageIcon image = new ImageIcon("../images/calderglen/totemPole.jpg", "description");
        textArea.insertIcon(image); // the JTextPane

For some reason, even though its on the same domain, Im getting a java.security.AccessControlException:

java.security.AccessControlException: access denied (java.io.FilePermission http://www.zzz.co.uk/images/calderglen/totemPole.jpg read)
      at java.security.AccessControlContext.checkPermission(Unknown Source)
      at java.security.AccessController.checkPermission(Unknown Source)
      at java.lang.SecurityManager.checkPermission(Unknown Source)
.....
.....
.....

Any ideas on why I cant access the image?

colr__
0
Comment
Question by:colr__
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 5
  • 4
  • +1
21 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17032401
That may be since you have an implied

cd ..

Put the images in or below your codebase
0
 
LVL 8

Author Comment

by:colr__
ID: 17032476
I've tried accessing the file from the same folder the jar is running, but still no luck. The problem is that the image is outside the jar, the applet html is as follows:

<applet archive="sands5.jar" code="newsletter.editor.SansEditor" width="750" height="420"></applet>

So ive got:

www.zzz.co.uk/theApplet.htm
www.zzz.co.uk/totemPole.jpg.

Its still giving me the same access refusal.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17032503
Try

ImageIcon image = new ImageIcon("/images/calderglen/totemPole.jpg", "description");

and put attribute

codebase="."

into the applet tag
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 86

Expert Comment

by:CEHJ
ID: 17032509
Or

ImageIcon image = new ImageIcon("/totemPole.jpg", "description");
0
 
LVL 92

Expert Comment

by:objects
ID: 17034058
you don't need (or want) to specify an absolute path, relative is the way to go.
you shouldn't need to specify the codebase either.

you problem looks more related to the setup at your isp, can you try running it on a different server or locally.
0
 
LVL 35

Expert Comment

by:girionis
ID: 17035268
You shouldn't, but if all else fails try to give FilePermission to the applet.
0
 
LVL 8

Author Comment

by:colr__
ID: 17035684
Ive tried using a relative path and still no luck, Ive also tried setting the codebase, but Im still getting the same error.

objects - what is likely to be stopping this at the ISP level, since the image file is on a public facing web server?

girionis  - this will mean signing the applet (am I wrong?) which would only be an 'if all else fails' route (I really dont want to have to do this).
0
 
LVL 35

Expert Comment

by:girionis
ID: 17035720
> girionis  - this will mean signing the applet (am I wrong?) which would only be an 'if all else fails' route (I really dont want to have to do this).

It will also mean changing the policy file, recommended if you only want to use the applet in the intranet.
0
 
LVL 92

Expert Comment

by:objects
ID: 17035849
> objects - what is likely to be stopping this at the ISP level, since the image file is on a public facing web server?

how they are handling redirection etc.
have u tried it on a local server?
0
 
LVL 8

Author Comment

by:colr__
ID: 17035862
Not yet, I Im not working on this project at the moment, Ill try it tonight.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 17036562
Actually i meant you to do


ImageIcon image = new ImageIcon(getClass().getResource("/totemPole.jpg"), "description");
0
 
LVL 8

Author Comment

by:colr__
ID: 17036569
With totemPole.jpg in the same folder as the jar? Ill try that.
0
 
LVL 8

Author Comment

by:colr__
ID: 17038092
Ok, the getResource() post by CEHJ stops the exception being thrown, but I'm unable to view the file in the JTextArea. If I extract the HTML from the HTMLEditorKit, I can see the code '<p $ename="icon">' inserted where the image should be, but it isnt displaying for some reason in the view?
0
 
LVL 35

Expert Comment

by:girionis
ID: 17038132
How do you add it to the jtextarea?
0
 
LVL 8

Author Comment

by:colr__
ID: 17038154
textArea = new JTextPane();
textArea.setEditable(true);
textArea.setEditorKit(new HTMLEditorKit());
...
ImageIcon image = new ImageIcon(getClass().getResource("/totemPole.jpg"), "description");
textArea.insertIcon(image);
0
 
LVL 35

Accepted Solution

by:
girionis earned 400 total points
ID: 17038184
Try to wrap the image in a Style first:

// The image must first be wrapped in a style
        Style style = doc.addStyle("StyleName", null);
        StyleConstants.setIcon(style, new ImageIcon("imagefile"));
// Insert the image at the end of the text
        doc.insertString(doc.getLength(), "ignored text", style);

from http://javaalmanac.com/egs/javax.swing.text/tp_ImageText.html
0
 
LVL 35

Expert Comment

by:girionis
ID: 17038186
And make sure that image is indeed loaded (it is not null).
0
 
LVL 8

Author Comment

by:colr__
ID: 17038380
What Im getting now is a different tag added to the document, '<StyleName $ename="icon">' and 'ignored text' show on the view.

Could this be anything to do with the fact that Im using an HTMLEditorKit? All the examples Ive seen posted on the 'net seem to do it quite simpy, but none of them Ive seen use HTMLEditorKit

objects - Trying it locally Im getting the exact same problem.
0
 
LVL 8

Author Comment

by:colr__
ID: 17038494
Got it working, with the following:

StyledDocument doc = (StyledDocument)textArea.getDocument();
textArea.getEditorKit().read(new StringReader("<img src=\"" + url + "\"></img>"), doc, textArea.getSelectionStart());
0
 
LVL 8

Author Comment

by:colr__
ID: 17038517
One thing Ive just noticed, it isnt adding the image close tag '</img>' to the document, anyone know why?
0
 
LVL 8

Author Comment

by:colr__
ID: 17038542
Got it:

textArea.getEditorKit().read(new StringReader("<img src=\"" + url + "\">"), doc, textArea.getSelectionStart());
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question