我正在尝试在 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 中将此代码作为小程序运行,它运行良好,但是如果我试图将它嵌入到网页中运行,我只会得到“CODE:”。代码卡住在这一行:
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
有人知道我做错了什么吗?谢谢!
最佳答案
这是签名的小程序。未签名的小程序可以从它们的来源服务器打开网络连接。 Applet 安全模型不允许打开到非源服务器的网络连接
看这里http://docs.oracle.com/javase/tutorial/deployment/applet/security.html
要确认这一点,您可以尝试从托管此小程序的服务器打开流。
关于java - URL 连接在小程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10766891/