我一直在开发一个音频播放器小程序,但在让它在网络浏览器中运行时遇到了问题。它在 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;
}
}
我已经尝试签署小程序,但这不起作用,它授予从网页查看小程序的人访问文件的权限。我也尝试编辑策略文件,但我无法让它工作,但它仍然是一个选项。
我已经遇到了几个小时的麻烦,不知道该怎么做,任何帮助将不胜感激。