File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to get a File object from a InputStream or BufferedReader? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to get a File object from a InputStream or BufferedReader?" Watch "How to get a File object from a InputStream or BufferedReader?" New topic
Author

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

prajal Mehta
Ranch Hand

Joined: Dec 03, 2007
Posts: 66
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 ]
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
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

Joined: Dec 03, 2007
Posts: 66
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
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
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

Joined: Dec 03, 2007
Posts: 66
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 ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
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.


Ping & DNS - my free Android networking tools app
prajal Mehta
Ranch Hand

Joined: Dec 03, 2007
Posts: 66
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
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
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

Joined: Dec 03, 2007
Posts: 66
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
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get a File object from a InputStream or BufferedReader?