tags:

views:

691

answers:

3

Hi there I want to read out a file that lies on the server. I get the path to the file by a parameter

<PARAM name=fileToRead value="http://someserver.de/file.txt"&gt;

when I now start the applet following error occurs

Caused by: java.lang.IllegalArgumentException: URI scheme is not "file"

Can someone give me a hint?

BufferedReader file;
                        String strFile = new String(getParameter("fileToRead"));

                        URL url = new URL(strFile);
                        URI uri = url.toURI();
                        try {

                            File theFile = new File(uri);
                            file = new BufferedReader(new FileReader(new File(uri)));

                        String input = "";

                            while ((input = file.readLine()) != null) {
                               words.add(input);
                            }
                        } catch (IOException ex) {
                          Logger.getLogger(Hedgeman.class.getName()).log(Level.SEVERE, null, ex);
                        }
+1  A: 

You are trying open as a file, something which doesn't follow the file:// uri, as the error suggests.

If you want to use a URL, I suggest you just use url.openStream() which should be simpler.

Peter Lawrey
ok i did it by accessing the file as a ressource, now it works :)
Xelluloid
+2  A: 
 File theFile = new File(uri);

is not the correct method. You accessing an URL, not a File.

Your code should look like this:

try
{
 URL url = new URL(strFile);
 InputStream in = url.openStream();
 (... read file...)
 in.close();
} catch(IOException err)
{
 (... process error...)
}
Pierre
ok I did it this way, in netbeans it works but opening the applet in the browser does not read the data. what can be wrong here?
Xelluloid
java.security.AccessControlException: access denied (java.net.SocketPermission root.xelluloid.de:80 connect,resolve)seems to be a security problem?
Xelluloid
By default applets and WebStart applications can only access the server they were downloaded from (a "same origin" policy). If an applet is loaded from the file system, there is no network origin.
Tom Hawtin - tackline
BTW: That should be final InputStream in = url.openStream(); try { (... read file...) } finally { in.close(); }
Tom Hawtin - tackline
You can work around that policy by packaging it as JAR and then signing it. The user will get a confirmation dialog where he has to allow extended access for your applet (trust it). You can access his full filesystem and open connections to any server afterwards.
Energiequant
I made the file a ressource and so it worked finde without a security message when signing it =) but thanks for all
Xelluloid
+1  A: 

You will need to sign the applet unless the file is being accessed from the same server/port that the applet came from.

http://forums.sun.com/thread.jspa?threadID=174214

TofuBeer