read file in an applet

自闭症网瘾萝莉.ら 提交于 2020-01-05 23:17:42

问题


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">

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:


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.




回答2:


 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...)
}



回答3:


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



来源:https://stackoverflow.com/questions/574675/read-file-in-an-applet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!