[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

Applet accessig image URL on same domain

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
colr__
Asked:
colr__
  • 10
  • 5
  • 4
  • +1
2 Solutions
 
CEHJCommented:
That may be since you have an implied

cd ..

Put the images in or below your codebase
0
 
colr__Author Commented:
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
 
CEHJCommented:
Try

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

and put attribute

codebase="."

into the applet tag
0
Technology Partners: 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!

 
CEHJCommented:
Or

ImageIcon image = new ImageIcon("/totemPole.jpg", "description");
0
 
objectsCommented:
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
 
girionisCommented:
You shouldn't, but if all else fails try to give FilePermission to the applet.
0
 
colr__Author Commented:
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
 
girionisCommented:
> 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
 
objectsCommented:
> 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
 
colr__Author Commented:
Not yet, I Im not working on this project at the moment, Ill try it tonight.
0
 
CEHJCommented:
Actually i meant you to do


ImageIcon image = new ImageIcon(getClass().getResource("/totemPole.jpg"), "description");
0
 
colr__Author Commented:
With totemPole.jpg in the same folder as the jar? Ill try that.
0
 
colr__Author Commented:
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
 
girionisCommented:
How do you add it to the jtextarea?
0
 
colr__Author Commented:
textArea = new JTextPane();
textArea.setEditable(true);
textArea.setEditorKit(new HTMLEditorKit());
...
ImageIcon image = new ImageIcon(getClass().getResource("/totemPole.jpg"), "description");
textArea.insertIcon(image);
0
 
girionisCommented:
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
 
girionisCommented:
And make sure that image is indeed loaded (it is not null).
0
 
colr__Author Commented:
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
 
colr__Author Commented:
Got it working, with the following:

StyledDocument doc = (StyledDocument)textArea.getDocument();
textArea.getEditorKit().read(new StringReader("<img src=\"" + url + "\"></img>"), doc, textArea.getSelectionStart());
0
 
colr__Author Commented:
One thing Ive just noticed, it isnt adding the image close tag '</img>' to the document, anyone know why?
0
 
colr__Author Commented:
Got it:

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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 10
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now