我在java applet中有简单的文件夹选择器。 applet成功打开,显示AccessControlException。我试图设置默认路径$ {user.home},然后用户可以选择他们想要的任何flder。如果我设置用户主路径以外的其他路径,则会发生相同的错误。我为此搜索了很多内容,对我没什么用。请查看以下跟踪并提出任何解决方案。
以下是来自jsp的小程序调用区域:
<td align="right">
<input type ="text" name="hdn_folder_path" readonly value="<%if(request.getRemoteAddr().toString().trim().equals(db_ip_address)){ out.println(hdn_folder_path);}%>" id="hdn_folder_path" size="60" class="textbox"> <input type="button" class="buttonorange" value="Choose.." onClick = "getFolder();" onmouseover="this.style.color='#fbe249';" onmouseout="this.style.color='#FFF';">
<OBJECT ID="FolderChooserApplet" NAME="FolderChooserApplet" WIDTH=00 HEIGHT=0 border="0" <% if(agentText.indexOf("msie") != -1){%>CLASSID="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" <%} else { %> CLASSID="java:JFolderChooser.JFolderChooser.class"<% } %> type="application/x-java-applet">
<PARAM NAME="code" value="JFolderChooser.JFolderChooser.class">
<PARAM NAME="archive" value="<%=session.getAttribute("APPLET_PATH")%>/JFolderChooser.jar">
<param name="defaultDir" value="<%if(request.getRemoteAddr().toString().equals(db_ip_address)){ out.println(hdn_folder_path);}%>">
<param name="mayscript" value="true">
</OBJECT>
</td>
Exception in thread "Basic L&F File Loading Thread" java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\Documents and Settings\Users" "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.exists(Unknown Source)
at sun.awt.shell.ShellFolder.getShellFolder(Unknown Source)
at javax.swing.filechooser.FileSystemView.getShellFolder(Unknown Source)
at javax.swing.filechooser.FileSystemView.getFiles(Unknown Source)
at javax.swing.plaf.basic.BasicDirectoryModel$LoadFilesThread.run0(Unknown Source)
at javax.swing.plaf.basic.BasicDirectoryModel$LoadFilesThread.run(Unknown Source)
JFolderChooser.java
import java.applet.Applet;
import java.io.File;
import java.security.AccessController;
import java.security.PrivilegedAction;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
public class JFolderChooser extends Applet {
/**
*
*/
private static final long serialVersionUID = -6499964033850797167L;
int returnVal;
public String folderPath = "", defaultPath = "c:\\", defaultDir = "";
public String formName = "", fieldName = "";
JFileChooser jfc;
@SuppressWarnings("unchecked")
@Override
public void init() {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
}
// defaultPath = getParameter("defaultDir");
// if (defaultPath == null) {
// defaultPath = System.getProperty("user.home");
// }
File defaultDir = new File(defaultPath);
if (!defaultDir.exists()) {
System.out.println("The specified folder does not exist.");
defaultDir = new File(System.getProperty("user.home"));
}
jfc = new JFileChooser();
jfc.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory();
}
@Override
public String getDescription() {
return "Choose Folder";
}
});
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.setCurrentDirectory(defaultDir);
jfc.setAcceptAllFileFilterUsed(false);
return null;
}
});
// getFolderSelected();
}
public String getFolderSelected() {
returnVal = jfc.showDialog(this, "Select");
if (returnVal == JFileChooser.APPROVE_OPTION) {
folderPath = jfc.getSelectedFile().getAbsolutePath();
System.out.println("You chose to open this folder: "
+ jfc.getSelectedFile().getAbsolutePath());
} else {
folderPath = defaultPath;
System.out.println("Rejected by User" + folderPath);
}
return folderPath;
}
public static void main(String args[]) {
}
}
注意: 我做了以下事情,
使用此命令签名jar
keytool -genkey -validity 3650 -keystore pKeyStore -alias keyName keytool -selfcert -keystore pKeyStore -alias keyName-validity 3650 jarsigner -keystore pKeyStore AppletClass.jar keyName
在$ {java.home} /jre/lib/security/java.policy中编辑了策略文件并添加了所有权限。通过这样做,applet成功运行并选择我的机器/计算机中的文件夹,但不在其他机器中。这应该适用于所有客户端计算机。 我不知道如何/在何处将策略文件添加到项目或应用程序中。
在java文件中添加了特权操作类。并粘贴下面的代码。
当我正常运行applet而不是从jsp调用时,它运行正常。
我使用的是java版:jdk1.6.0_12和jre6
感谢。
编辑:
最后这个工作,
我更改了从javascript调用的函数getFolderSelected()
,
public String getFolderSelected(){
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
returnVal=jfc.showDialog(JFolderChooser.this, "Select");
if(returnVal == JFileChooser.APPROVE_OPTION) {
folderPath = jfc.getSelectedFile().getAbsolutePath();
System.out.println("You chose to open this folder: " + jfc.getSelectedFile().getAbsolutePath());
}else {
folderPath = defaultPath;
System.out.println("Rejected by User"+folderPath);
}
return folderPath;
}
});
return folderPath;
}
答案 0 :(得分:0)
好像你的小程序试图从LFS获得JRE L&amp; F
C:\ Documents and Settings \ Users
...而且,将"c:\\"
作为默认路径并不是一个好主意,因为通常情况下,它不是当前的OS用户会话根目录(至少对于Windows)。 ..
回到堆栈跟踪......这可能是L&amp; F问题。因此,尝试使用默认的L&amp; F或打包(进入jar文件)将Windows L&amp; F与applet一起运行applet,而不是读取客户端的。
P.S。我在这里不太确定...但请检查您的Internet浏览器Java设置。我记得,Oracle最近限制了一些默认的applet选项,所以你可能需要手动设置它们:S
报告是否有帮助