是否可以创建一个能够访问其私人计算机上用户文件的Java小程序?
我有一个自签名且加载很好的Java applet,但是当我尝试选择一个文件时,它不起作用并给我以下异常:
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] Exception in thread "AWT-EventQueue-2" java.security.AccessControlException: access denied
(java.util.PropertyPermission user.home read)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.security.AccessController.checkPermission(AccessController.java:546)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:1285)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.lang.System.getProperty(System.java:667)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at sun.awt.shell.ShellFolderManager.get(ShellFolderManager.java:56)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at sun.awt.shell.ShellFolder.get(ShellFolder.java:227)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at javax.swing.filechooser.FileSystemView.getDefaultDirectory(FileSystemView.java:391)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at javax.swing.JFileChooser.setCurrentDirectory(JFileChooser.java:552)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at javax.swing.JFileChooser.<init>(JFileChooser.java:334)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at javax.swing.JFileChooser.<init>(JFileChooser.java:316)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at ca.file.FileUtils.getFile(FileUtils.java:175)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at ca.file.FileUtils.getStructureFile(FileUtils.java:288)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at ca.uielements.Core.getStructureFile(Core.java:421)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at ca.uielements.Core.actionPerformed(Core.java:444)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at javax.swing.AbstractButton.doClick(AbstractButton.java:389)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1223)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at com.apple.laf.AquaMenuItemUI.doClick(AquaMenuItemUI.java:137)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1264)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.Component.processMouseEvent(Component.java:6348)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at javax.swing.JComponent.processMouseEvent(JComponent.java:3255)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.Component.processEvent(Component.java:6113)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.Container.processEvent(Container.java:2085)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.Component.dispatchEventImpl(Component.java:4714)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.Container.dispatchEventImpl(Container.java:2143)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.Component.dispatchEvent(Component.java:4544)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.Container.dispatchEventImpl(Container.java:2129)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.Window.dispatchEventImpl(Window.java:2475)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.Component.dispatchEvent(Component.java:4544)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
14-01-08 10:41:15 PM [0x0-0x148148].com.apple.Safari[2987] atjava.awt.EventDispatchThread.run(EventDispatchThread.java:122)
问题是我在崩溃的行中创建了一个新的JFileChooser
。这个小程序的原始观点是允许用户查看某些结构文件而无需下载软件,只需上线并使其工作。但是,我没有资金支付专业证书,也不知道我的applet是否可以访问用户的文件呢?
这是一个简单的applet类,用于演示此问题:
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.io.File;
public class simpleApplet extends JApplet {
static final String message = "Shlonsky!";
private Font font = new Font("serif", Font.ITALIC + Font.BOLD, 36);
public void init() {
// set the default look and feel
String laf = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(laf);
} catch (UnsupportedLookAndFeelException exc) {
System.err.println ("Warning: UnsupportedLookAndFeel: " + laf);
} catch (Exception exc) {
System.err.println ("Error loading " + laf + ": " + exc);
}
getContentPane().setLayout (null);
}
public void start()
{
JFrame vizFrame = new JFrame("myFrame");
vizFrame.setBackground(Color.white);
vizFrame.getContentPane().setBackground(Color.white);
vizFrame.setSize(800, 650);
vizFrame.setVisible(true);
File previousFile = null;
JFileChooser chooser = new JFileChooser(previousFile); // This is where the program would crash
int fileReturnVal = chooser.showOpenDialog(vizFrame);
}//end start
public void paint (Graphics g) {
super.paint(g);
g.setColor(Color.blue);
g.setFont(font);
g.drawString(message, 40, 80);
}
}
编辑:非常奇怪,我刚刚签署了我制作的简单课程,当我运行它时,Safari会问我是否要信任applet,但由于某种原因它不会对其他applet这样做(以同样的方式签署)。我正在使用的HTML文件中是否存在阻止Safari要求批准的内容?