java – URL连接在applet中不起作用

我试图在
java applet中运行此代码:

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运行此代码作为applet,它运行正常,但是如果我试图在网页中嵌入它运行它我只得到“代码:”.代码冻结在这一行:

BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));

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

最佳答案 这是签名小程序.未签名的Applet可以从发起它们的服务器打开网络连接.然后,Applet安全模型不允许打开与非始发服务器的网络连接

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

要确认这一点,您可以尝试从托管此applet的服务器打开流.

点赞