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
Solved

Applet accessig image URL on same domain

Posted on 2006-07-03
21
409 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

808 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