Solved

Applet accessig image URL on same domain

Posted on 2006-07-03
21
382 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__
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now