无法从JSP访问包含JFileChooser的Applet

时间:2011-09-26 16:53:02

标签: jsp applet

我编写了以下简单的Applet,它提供了一个File Uploader:

import java.io.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FileChooserDemo extends Applet implements ActionListener {
static private final String newline = "\n";
JButton openButton;
JTextArea log;
JFileChooser fc;

public void init() {


    // Create the log first, because the action listeners
    // need to refer to it.
    log = new JTextArea(5, 20);
    log.setMargin(new Insets(5, 5, 5, 5));
    log.setEditable(false);
    JScrollPane logScrollPane = new JScrollPane(log);

    // Create a file chooser
    fc = new JFileChooser();

    // Uncomment one of the following lines to try a different
    // file selection mode. The first allows just directories
    // to be selected (and, at least in the Java look and feel,
    // shown). The second allows both files and directories
    // to be selected. If you leave these lines commented out,
    // then the default mode (FILES_ONLY) will be used.
    //
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    // fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

    // Create the open button. We use the image from the JLF
    // Graphics Repository (but we extracted it from the jar).
    openButton = new JButton("Open a File...");
    openButton.addActionListener(this);



    // For layout purposes, put the buttons in a separate panel
    JPanel buttonPanel = new JPanel(); // use FlowLayout
    buttonPanel.add(openButton);


    // Add the buttons and the log to this panel.
    add(buttonPanel, BorderLayout.PAGE_START);
    add(logScrollPane, BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e) {

    // Handle open button action.
    if (e.getSource() == openButton) {
        int returnVal = fc.showOpenDialog(FileChooserDemo.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            // This is where a real application would open the file.
            log.append("Opening: " + file.getName() + "." + newline);
        } else {
            log.append("Open command cancelled by user." + newline);
        }
        log.setCaretPosition(log.getDocument().getLength());

        // Handle save button action.
    } 
}




}

除非我尝试将其包含在Web应用程序中,否则它运行正常。我将它编译成JAR并签署了JAR。下面是引用Applet的JSP文件:

<%@ page language="java" %>
<html>
<head>
<title>Welcome JSP-Applet Page</title>
</head>
<body>
<jsp:plugin type="applet" code="FileChooserDemo" codebase="./applet" archive="Applet.jar" 
  width="400" height="400">
  <jsp:fallback>
   <p>Unable to load applet</p>
   </jsp:fallback>
</jsp:plugin>
</body>
</html>

当我尝试从浏览器打开时。我从Java控制台窗口中收到以下错误:

java.security.AccessControlException: access denied (java.io.FilePermission C:\Users\jatoui\Documents 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 java.io.Win32FileSystem.canonicalize(Unknown Source)
    at java.io.File.getCanonicalPath(Unknown Source)
    at sun.awt.shell.Win32ShellFolderManager2.createShellFolder(Unknown Source)
    at sun.awt.shell.Win32ShellFolderManager2.getPersonal(Unknown Source)
    at sun.awt.shell.Win32ShellFolderManager2.get(Unknown Source)
    at sun.awt.shell.ShellFolder.get(Unknown Source)
    at javax.swing.filechooser.FileSystemView.getDefaultDirectory(Unknown Source)
    at javax.swing.JFileChooser.setCurrentDirectory(Unknown Source)
    at javax.swing.JFileChooser.<init>(Unknown Source)
    at javax.swing.JFileChooser.<init>(Unknown Source)
    at com.blackducksoftware.proserv.applets.FileChooserDemo.init(FileChooserDemo.java:26)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Exception: java.security.AccessControlException: access denied (java.io.FilePermission C:\Users\jatoui\Documents read)

有没有办法克服这个???

1 个答案:

答案 0 :(得分:1)

java.security.AccessControlException: access denied 
    (java.io.FilePermission C:\Users\jatoui\Documents read)

applet需要由开发人员进行数字签名,并且在最终用户的计算机上读取或写入文件之前,最终用户需要信任。 (除非它部署在插件2 JRE中,并使用JNLP API服务访问本地文件系统。