【发布时间】:2013-09-11 19:53:37
【问题描述】:
我有一个用于 Intranet 站点的 Java 小程序,它需要访问客户端 PC 上的文件系统(特别是 LAN 共享)。这个小程序的功能类似于:
JSObject win;
public void init() {
win=(JSObject) JSObject.getWindow(this);
win.call("appletMsg", new Object[] {"<b>Applet Loaded</b>", "win"});
}
public void saveFile(String filepath, String filename) {
File theDir = new File(filepath);
try {
if (theDir.exists()) { // This throws exception
win.call("appletMsg", new Object[] {"Directory Exists", "win"});
}
else {
win.call("appletMsg", new Object[] {"Creating Directory...", "msg"});
if (theDir.mkdir()) {
win.call("appletMsg", new Object[] {"Directory Created", "win"});
}
else win.call("appletMsg", new Object[] {"Directory Creation Failed!", "fail"});
}
}
catch (Exception e) { // This exception is caught
win.call("appletMsg", new Object[] {"Error Reading Directory!", "fail"});
win.call("appletMsg", new Object[] {filepath, "fail"});
}
// More code for working with files, error happens above this
}
小程序背后的Javascript
// call applet method
function save() {
document.myApplet.saveFile('\\\\LOCATION\\DIR\\DIR\\', 'test.txt');
}
// output responses from applet to div
function appletMsg(response, type) {
document.getElementById('output').innerHTML+='<br><span class='+type+'>'+response+'</span>';
}
疑难解答/想法:
- 小程序在我让 JS 调用 Java 方法之前工作 (参数在参数列表中,小程序完全重新加载时 需要,文件操作在 init() 方法中,恢复到这个工作但很糟糕的做法)
- 小程序使用自证书签名
- 我在 init() 方法中放置了一个 JFileChooser 以确保路径是 对,把它移到 saveFile() 方法,对话框没有 显示。它不会像使用 .exists() 在 Java 中调用,它确实会在 尝试/捕捉
- 因为这在 init() 中有效,在 saveFile() 中无效,所以我只能假设 这是为了防止 JavaScript 本身访问文件系统??
【问题讨论】:
-
如果您使用的是不受信任的(即自签名)证书,浏览器将不允许您访问文件系统。
-
他们这样做,只是在 init() 方法中。有两种可能的解决方法,如 Yann39 指出的 doPrivilieged,或者在 init() 中定义一个执行回调的线程。
标签: java javascript security applet sandbox