我正在尝试在 Java 小程序中运行此代码:

package test;
import java.applet.Applet;

import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class DrawExample extends Applet {
    public void paint(Graphics g) {
        try {
            g.drawString("CODE:",50, 30);
            URL yahoo = new URL("http://www.yahoo.com/");
            URLConnection yc = yahoo.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
            String inputLine;
            int i=65;
            while ((inputLine = in.readLine()) != null) {
                g.drawString(inputLine,50, i);
                i=i+15;
            }
            in.close();
         } catch (Exception e) {
             e.printStackTrace();
         }
    }
}

如果我从 Eclipse 作为小程序运行此代码,它运行良好,但是如果我试图运行它嵌入在网页中,我只会得到“代码:”。代码在这一行卡住:
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));

任何人都知道我做错了什么?谢谢!

最佳答案

这是签名的小程序吗?未签名的 Applet 可以从它们起源的服务器打开网络连接。 Applet 安全模型不允许再打开与非源服务器的网络连接

看看这里 http://docs.oracle.com/javase/tutorial/deployment/applet/security.html

要确认这一点,您可以尝试从托管此小程序的服务器打开流。

关于java - URL 连接在小程序中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10766891/

10-11 02:56