如何从java程序中获取本地硬盘中html文件的执行结果?

发布于 2024-10-07 15:19:28 字数 798 浏览 5 评论 0原文

我试图第一次将硬盘上的 html 文件作为字符串(即运行文件中显示的类型)执行的结果形成 java 程序,但似乎我没有得到正确的结果。

这是代码:

import java.io.*; 

import java.net.URL;

public class MapDir  {

public static void main(String[] args) throws FileNotFoundException, IOException
{

    String s = "file:///F:/Stuff/Muaz/Programming/Mobile Applications/J2ME/Ride Sharing  System/RSS Server/routeDistance.html?addr1=30.065634,31.209473&addr2=29.984177,31.440052";
    URL url = new URL("file", "Core", s);

   BufferedReader r = new BufferedReader(new InputStreamReader(url.openStream()));

   while((s = r.readLine()) != null)
   {
      System.out.println(s);
   }
  }
}

当我运行代码时,出现以下错误:

java.net.ConnectException:连接被拒绝:连接....

当然,我确信字符串 File (s)。我从浏览器中运行它并且运行良好。主机名也是正确的。那怎么了?请尽快回复。提前致谢。

I'm trying to get the result of executing an html file on my hard disk as a string (thats the type of whats displayed in running the file) form a java program for the first time and it seems i did not get it right.

here is the code:

import java.io.*; 

import java.net.URL;

public class MapDir  {

public static void main(String[] args) throws FileNotFoundException, IOException
{

    String s = "file:///F:/Stuff/Muaz/Programming/Mobile Applications/J2ME/Ride Sharing  System/RSS Server/routeDistance.html?addr1=30.065634,31.209473&addr2=29.984177,31.440052";
    URL url = new URL("file", "Core", s);

   BufferedReader r = new BufferedReader(new InputStreamReader(url.openStream()));

   while((s = r.readLine()) != null)
   {
      System.out.println(s);
   }
  }
}

when I run the code I get the following error:

java.net.ConnectException: Connection refused: connect....

Of-course Im sure of the string File (s). I ran it form a browser and it works perfectly. The host name is also correct. So whats wrong? Please reply back as soon as you can. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城 2024-10-14 15:19:28

检查 文档以获取 URL,你……

String s = "file://location";
URL url = new URL("file", "Core", s);

初始化不正确。查看此链接

干杯!

编辑:好的,评论有点长。

  • 首先,据我所知,URL 类没有执行 html 文件的方法。或者任何类,就此而言。

  • 其次,您的代码正在读取该文件,如果没有我们指出的错误,它就会工作。我怎么知道这实际上不是你想要做的?我认为标题的英文很糟糕。

  • 第三,没有人付钱让我们回答你的问题,所以不要咆哮,而是尝试感谢。与您原来的帖子中“尽快回答”相同的态度不会给您带来支持或帮助。

啊。我不得不这么说。我会接受任何反对票作为惩罚。

编辑2:(

来自我下面的评论)@Muaz HTML 只是数据。没有人“运行”HTML 文件,就像您不运行 .doc 或 .avi 一样——您运行另一个程序,它知道如何解释这些数据以及如何处理它。该程序可以是网络浏览器、Microsoft Word 或 VLC 媒体播放器;但无论如何,正在执行的不是数据文件。 –

Check the documentation for URL, you are...

String s = "file://location";
URL url = new URL("file", "Core", s);

... initializing it incorrectly. Take a look at this link.

Cheers!

Edit: ok, this is a bit long for a comment.

  • First, the URL class has no method of executing an html file that I know of. Or any class, for that matter.

  • Second, your code is READING the file, and it would work if it wasn't for the error we pointed out. How am I supposed to know that's not actually want you want to do? I thought the title was just bad english.

  • Third, nobody is paying us to answer your questions, so instead of barking, try thanking. The same attitude as in "answer as soon as you can" in your original post won't get you support or help.

Ah. I just had to say that. I'll accept any downvotes as just punishment.

Edit 2:

(From my comment below) @Muaz an HTML is just data. Nobody "runs" an HTML file, the same way you don't run a .doc, or a .avi -- you run another program that knows how to interpret this data and what to do with it. That program can be a web browser, Microsoft Word, or the VLC media player; but whatever the case, it's not the data file the one that's being executed. –

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文