2009-02-22 74 views
1

嗨,我想读出位于服务器上的文件。 我得到的文件路径由参数在小程序中读取文件

<PARAM name=fileToRead value="http://someserver.de/file.txt"> 

当我现在开始小程序出现下列错误

造成的:java.lang.IllegalArgumentException异常:URI方案不是“文件”

有人可以给我一个提示吗?

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); 
         } 
+0

请显示一些代码,以及您给applet的URL。 – 2009-02-22 10:00:36

回答

1

你试图打开一个文件,不遵循下面的文件:// uri,正如错误所示。

如果你想使用一个URL,我建议你只使用url.openStream(),它应该更简单。

+0

好吧,我通过访问该文件作为资源,现在它工作:) – 2009-02-22 11:33:55

3
File theFile = new File(uri); 

不是正确的方法。您访问的是网址,而不是文件。

您的代码应该是这样的:

try 
{ 
URL url = new URL(strFile); 
InputStream in = url.openStream(); 
(... read file...) 
in.close(); 
} catch(IOException err) 
{ 
(... process error...) 
} 
+0

好吧我这样做,在netbeans它的工作,但在浏览器中打开小程序不读取数据。这里有什么可能是错的? – 2009-02-22 10:11:49

1

您将需要除非该文件从该小程序的来源相同的服务器/端口的访问对Applet签名。