2011-09-26 13 views
1

我寫了下面的簡單的小程序,提出了一個文件上傳:無法訪問的Applet JFileChooser的含JSP從

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) 

有什麼辦法可以解決這個問題?

+0

您可以參考此文檔http://stackoverflow.com/questions/4850351/applet-and-jsp -communication/27358112#27358112祝您好運.......... –

回答

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

一個applet需要進行數字由開發商由最終用戶簽署,並值得信賴的,纔可以閱讀或最終用戶機器上寫入文件。 (除非它部署在插件2 JRE中,並且使用JNLP API Services訪問本地文件系統。

+0

如果您錯過了原始帖子中的備忘錄:「我將它編譯成JAR並簽署了JAR。」 – reddaj

+0

您是否被提示信任代碼?是否由JavaScript調用代碼? –

+0

此提示未出現,並且通過標記在JSP內引用代碼 – reddaj