• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How to get a File object from a InputStream or BufferedReader?

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Is it possible to get a File Object from a InputStream or BufferedReader?

It is basically to allow my applet running in a browser to access local file system. My applet has to access few files from my local drive.

I found following code on internet and want to get a File object from which I can call list() on.
I want to know if that is feasible.
Any help is appreciated.


[ August 02, 2008: Message edited by: prajal Mehta ]
 
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is some confusion about where the file is located. You say it is on the local disk, but the URL you show uses the http protocol which would go to a server.
Applets loaded from a server to a client's browser are NOT allowed to read files from that client's local disk without permission. The applet can read files from the server that it was loaded from. It will not be able get a File object for a server file and will not be able to do a File.list()

Could you describe the problem in more detail?
 
prajal Mehta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Norm, thanks for the reply.
Please could you help me with following code. That will help me take a step ahead in resolving my actual problem.

The applet when run on a browser is displaying an empty text area.
I expect it to read and display the contents of page located at given URL.
Am I doing something wrong?
 
Norm Radder
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look in the Java console for any error messages. Copy and paste them here.
Describe how you are loading the applet? local,server, browser, OS, etc
 
prajal Mehta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. There are no errors in JAVA Console.
2. I am using JDEVELOPER IDE .
I am loading this applet on a browser using using a JSP Plugin --


3. I am running it in embedded OC4J server.

I've just started learning applets.
Sorry if my questions sound foolish.
[ August 03, 2008: Message edited by: prajal Mehta ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Applets have a number of security restrictions. Norm mentioned the one about not being able to access the local file system. Another one is the inability to access hosts other than the one where the applet originated from. So a URL to java.sun.com is not allowed. You'll need to digitally sign the applet. See HowCanAnAppletReadFilesOnTheLocalFileSystem for more details.
 
prajal Mehta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:
Applets have a number of security restrictions. Norm mentioned the one about not being able to access the local file system. Another one is the inability to access hosts other than the one where the applet originated from. So a URL to java.sun.com is not allowed. You'll need to digitally sign the applet. See HowCanAnAppletReadFilesOnTheLocalFileSystem for more details.



Hi Ulf,

I went thru the given link-
I modified my java.policy file in jre1.6.0_03\lib\security\java.policy
I added following lines to it -


Then I ran my applet (not the one posted above )which tries to access a folder from my local drive.
But I still get this error-
java.security.AccessControlException: access denied (java.io.FilePermission D:\JDEV10133\jdev\mywork\NewAppletCode\ViewController\public_html\applet\textVrml1 read)

I do not have policytool and couldn't download from internet.

Please let me know if the above changes in policy file are sufficient or I need some changes in some other file too?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need to download policytool. It comes with the JDK.

How are you running the applet - from a file:/// URL, from a local web server, using appletviewer, ...?
 
prajal Mehta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:
You don't need to download policytool. It comes with the JDK.

How are you running the applet - from a file:/// URL, from a local web server, using appletviewer, ...?


From a local web server (embedded oc4j server)

how do I access the policytool?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by prajal Mehta:
From a local web server (embedded oc4j server)


Then the codebase wouldn't be "File:/D:/JDEV10133/jdev/mywork/NewAppletCode/-", it would be something like "http://localhost....".

how do I access the policytool?


It should be in the bin directory of your JDK installation.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic