Не удается получить доступ к апплету, содержащему JFileChooser, из JSP - PullRequest
1 голос
/ 26 сентября 2011

Я написал следующий простой апплет, который представляет загрузчик файлов:

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.
    } 
}




}

Он работает нормально, за исключением случаев, когда я пытаюсь включить его в веб-приложение.Я собрал его в JAR и подписал JAR.Ниже приведен файл 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 Ответ

1 голос
/ 28 сентября 2011
java.security.AccessControlException: access denied 
    (java.io.FilePermission C:\Users\jatoui\Documents read)

Апплет должен иметь цифровую подпись разработчика и быть доверенным конечному пользователю, прежде чем он сможет читать или записывать файлы на компьютере конечного пользователя.(Если только он не развернут в плагине 2 JRE и не использует службы API JNLP для доступа к локальной файловой системе.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...