0

I have simple folder chooser in java applet. The applet opens successfully and it shows AccessControlException. Im trying to set the default path ${user.home}, and then the user can select any flder they wish. If i set the other than user home path, the same error occurs. I have googled a lot for this and nothing works to me. Please look at the below traces and suggest any solution.

Below is the applet calling area from 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">&nbsp;&nbsp;<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>

Log:

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[]) {

    }
}

Note: I have done the following things,

  1. Signed jar using this commands

    keytool -genkey -validity 3650 -keystore pKeyStore -alias keyName keytool -selfcert -keystore pKeyStore -alias keyName-validity 3650 jarsigner -keystore pKeyStore AppletClass.jar keyName

  2. Edited policy file in ${java.home}/jre/lib/security/java.policy and added all permission. By doing this, the applet runs succesfully and choosing the folder in my machine/computer but not in others. This should work in all client machines. And i do not know how/where to add policy file to the project or application.

  3. Added privileged action classes in java file. and pasted the code below.

  4. When i run the applet normally instead calling from jsp, it works fine.

  5. Im using java version: jdk1.6.0_12 and jre6

Thanks.

EDITED: Finally this works, I changed the function getFolderSelected() which is calling from javascript,

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;
    }
4

1 回答 1

0

似乎您的小程序正试图从 LFS 获取 JRE L&F 作为

C:\文档和设置\用户

...而且,此外,将"c:\\"其设为默认路径并不是一个好主意,因为通常它不是当前操作系统用户会话根目录(至少对于 Windows 而言),无论如何...

回到堆栈跟踪......这可能是 L&F 问题。因此,尝试使用默认 L&F 运行小程序,或者将 Windows L&F 与您的小程序一起打包(到 jar 文件中),而不是读取客户端的。

PS 我不太确定这里...但是检查您的 Internet 浏览器 Java 设置。我记得,Oracle 最近限制了一些默认的小程序选项,所以也许你必须手动设置它们:S


报告是否有帮助

于 2013-03-19T19:39:49.413 回答