繁体   English   中英

如何使用getCodeBase()在Java Applet中查找和加载文件?

[英]How to find and load a file within a Java Applet using getCodeBase()?

第一次在这里发布,将尽量简洁。 这是一个经典的“无法在Applet中访问文件”问题,但是我遇到了一个特殊的困难。

我正在尝试重写此文件:

针对libpd的JavaSound测试

进入模板小程序以加载在PureData(puredata.info)中制作的libpd( https://github.com/libpd/libpd )补丁...这已经可以在非小程序Java程序的常规Main函数中工作了(请参见上文) ),其中主要功能使用以下命令找到补丁:

PdBase.openAudio(0, outChans, (int) sampleRate);
    int patch = PdBase.openPatch("samples/com/noisepages/nettoyeur/libpd/sample/test.pd");
    PdBase.computeAudio(true);

它尝试将路径和文件加载到int变量中的原因是,核心函数本身使用以下方法执行此操作:

public synchronized static int openPatch(File file) throws IOException {
    if (!file.exists()) {
        throw new FileNotFoundException(file.getPath());
    }
    String name = file.getName();
    File dir = file.getParentFile();
    long ptr = openFile(name, (dir != null) ? dir.getAbsolutePath() : ".");
    if (ptr == 0) {
        throw new IOException("unable to open patch " + file.getPath());
    }
    int handle = getDollarZero(ptr);
    patches.put(handle, ptr);
    return handle;
}
public synchronized static int openPatch(String path) throws IOException {
    return openPatch(new File(path));
}

这是因为PD会尝试通过提供一个int'handle'(出于传统原因,使用dollarZero)来识别每个补丁,以便将int句柄传递给打开和关闭补丁文件。

所以现在。 我正在尝试在Applet中加载相同的文件,因此由于我相信该文件在“客户端”中运行并且不知道我在说什么路径,因此我在java.net.URL上进行了阅读并尝试构建的变化:

        patchURL = new URL("test.pd");
    PdBase.openPatch(patchURL.getPath().toString());

URL url = this.getClass().getResource("test.pd");

受applet的init()和start()函数中先前问题的启发,将原始main转换为本地静态方法sound()。

我得到的只是空指针。 我以为我所需要的只是一个简单的getDocumentBase(),但似乎无法使其正常工作。 任何人?

libpd只是Pure Data之上的一个薄包装,而Pure Data不了解Java中的URL或输入流。 openPatch方法仅将修补程序名称和目录发送到Pd,然后Pd将尝试打开相应的文件。 因此,除非您愿意修改安全策略,否则applet会淘汰。

关于查找文件,简单的示例程序是libpd Eclipse项目的一部分。 它打算在Eclipse中运行,并且补丁的硬编码路径相对于Eclipse中的项目根目录。 如果希望代码在其他设置中运行,则必须相应地调整路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM