【发布时间】:2012-03-22 03:49:26
【问题描述】:
我正在尝试获取一个目录(applet 目录中名为 music 的子目录)中所有 mp3 文件的列表,以便我可以在 JavaScript 函数中将它们的名称推送到数组中。
一切正常,但列出过程......它只返回目录中的第一个 mp3 文件,而不是其他文件......
这是我的代码
JAVA:
import java.applet.Applet;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
public class Main extends Applet {
private static final long serialVersionUID = 1L;
public void init() {
File[] lib = getFiles(new File((getCodeBase() + File.separator + "music").substring(6)));
for (File s:lib) {
if (s.getName().substring(s.getName().length() - 3).equalsIgnoreCase("mp3")) {
try {getAppletContext().showDocument(new URL("javascript:addSong('"+s.getName()+"')"));}
catch (MalformedURLException me) {}
}
}
try {getAppletContext().showDocument(new URL("javascript:init()"));}
catch (MalformedURLException me) {}
}
public File[] getFiles(File dir) {
return dir.listFiles();
}
}
JavaScript:
function addSong(s) {
// Adding to array
window.songs.push("music/" + s);
// Debug message
alert(s);
}
function init() {
// Random code to initialze music player
// getting and listing values from "songs" which got content form addSong()
}
【问题讨论】:
-
您是否尝试过从提供此小程序的主机之外的另一台主机执行此小程序?您无法使用
new File()访问服务器上的文件。 -
@JBNizet 我正要说同样的话,但既然你已经说过了,我只会投票赞成评论作为回复中提供的第一个明智的事情。
-
“我正在本地尝试” 那又怎样?让它在本地工作,它会仍然在生产中失败。整个方法需要改变(并且可能放弃任务)。
-
它只应该在本地工作。
-
那你到底为什么要编写一个applet?小程序只有在通过网络或 Internet 部署给人们时才真正有意义。顺便说一句 - 这个小程序需要数字签名,普通应用程序不需要。
标签: java javascript applet directory subdirectory