?
Solved

Applet accessig image URL on same domain

Posted on 2006-07-03
21
Medium Priority
?
429 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 300 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 1200 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 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