English 中文(简体)
小程序中不工作的 URL 连接 URL URL URL
原标题:URL connection not working in 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的代码作为一个小程序运行,它就很好,但如果我试图在网页中运行,我只能得到“CODE:”的代码。

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

有人知道我做错了什么吗?

问题回答

这是签名的小程序。 未签名 Applet 可以从服务器打开网络连接, 从服务器上打开网络连接 。 Applet 安全模式不允许打开网络连接到非发源服务器 。

请在此查看

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

您需要签名您的 java applet。 当您在 Eclipse 运行时, 它会有效, 因为日蚀会生成一个名为 < 坚固 > java. policy. applet 的文件, 包含以下内容 。

/* AUTOMATICALLY GENERATED ON Tue Apr 16 17:20:59 EDT 2002*/
/* DO NOT EDIT */

grant {
  permission java.security.AllPermission;
};

为了让您的小程序在日圆外运行, 您必须签名 :

  1. Compile applet - javac Applet.java
  2. Make a JAR File - jar cvf Applet.jar Applet.class
  3. Generate Keys
  4. Sign the JAR File
  5. Export the Public Key Certificate ...

它持续了很长的过程。 您可以在此页 < http://java.sun.com/ developmenter/ online training/ Programming/ JDCBook/ ignignation.html#example" rel = "nofollow" 中跟踪所有步骤 < http://java. sun.com/ developer/ online training/ progragramming/ JDCBook/ signed.html#example" rel="nofollow" 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签