1

我一直在开发一个音频播放器小程序,但在让它在网络浏览器中运行时遇到了问题。它在 Eclipse 的小程序查看器中完美运行,但当我在 Google Chrome 中尝试时它不起作用。我什至尝试了eclipse生成的applet文件,但它也不起作用,可能与eclipse运行它的方式有关。

这是 html 文件,但不允许访问 mp3 文件或我制作的特殊字体:

<html>

<meta http-equiv="Content-Type" content="text/html; charset=MacRoman"/>

<body>

<applet code="DOSPlayer.class" archive="DOSPlayer.jar, basicplayer3.0.jar, commons-logging-1.1.1.jar, mp3spi1.9.5.jar, tritonus_share-0.3.6.jar, jl1.0.1.jar" width="450" height="350" >

</applet>

</body>

</html>

我得到的错误是因为访问权限它无法访问文件:

java.security.AccessControlException: access denied (java.io.FilePermission fonts\DOSFont.ttf read)
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkRead(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at DOSPlayer.initFont(DOSPlayer.java:398)
    at DOSPlayer.<init>(DOSPlayer.java:413)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
java.lang.reflect.InvocationTargetException
    at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.security.AccessControlException: access denied (java.io.FilePermission mp3 read)
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkRead(Unknown Source)
    at java.io.File.list(Unknown Source)
    at java.io.File.listFiles(Unknown Source)
    at DOSPlayer.getFiles(DOSPlayer.java:259)
    at DOSPlayer.<init>(DOSPlayer.java:415)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception: java.lang.reflect.InvocationTargetException

我知道错误来自这两个代码片段:

     //path is "mp3/"
public String[] getFiles(String path) {

              String[] songs = new String[501];
              String files;
              File folder = new File(path);
              File[] listOfFiles = folder.listFiles(); 
              //gets all mp3 files within mp3 folder
             try {
              for (int i = 0; i < listOfFiles.length; i++) {

               if (listOfFiles[i].isFile()) 
               {
               files = listOfFiles[i].getName();
                    if (files.endsWith(".mp3")) {
                            songs[songNum] = files;
                            songNum++;
                            playableSongs++;
                    }
                 }
              }
             } catch (Exception e) {
                 System.out.println("Error Here");
                 e.printStackTrace();
                 return null;
             }
              return songs;


}

/////////////////////////////////

public void initFont() {
        try {
            //URL fontUrl = new URL("fonts/DOSFont.ttf");
            File file = new File("fonts/DOSFont.ttf"); // where ever it is from

            FileInputStream fontPath = new FileInputStream(file);
            dosFont = Font.createFont(Font.TRUETYPE_FONT, fontPath);//fontUrl.openStream());
            dosFont = dosFont.deriveFont(Font.PLAIN,19);
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            ge.registerFont(dosFont);
            font = true;
                } catch(Exception e) {
                    e.printStackTrace();
                    font = false;
                }
    }

我已经尝试签署小程序,但这不起作用,它授予从网页查看小程序的人访问文件的权限。我也尝试编辑策略文件,但我无法让它工作,但它仍然是一个选项。

我已经遇到了几个小时的麻烦,不知道该怎么做,任何帮助将不胜感激。

4

1 回答 1

2

这些资源(尤其是字体)实际上是“应用程序资源”,可以添加到 Jar 中并添加到小程序的运行时类路径中。如果不在存档中,则应将它们添加到服务器上的公共场所(可以直接从浏览器访问它们)。

一旦进入 Jar 或服务器,就可以通过 URL 访问它们。如果没有嵌入到 Jar 中,则 URL 可以形成为代码或文档库的相对路径。如果它们在 Jar 中,请使用以下内容获取 URL:

URL urlToFont = this.getClass().getResource("/path/in/jar/to/the.ttf");

我也尝试编辑策略文件,但我无法让它工作,但它仍然是一个选项。

不,它不能。策略文件适用于非常有限的范围,公开部署的小程序不是其中之一。


只是对..的快速评论

File[] listOfFiles = folder.listFiles(); 

这需要重新考虑。如果没有服务器的帮助,小程序无法获取服务器上的文件列表。要定义一组文件,applet 通常会有一个接受路径/名称的参数。

于 2012-08-09T00:20:19.727 回答