Java 小程序 java.security.AccessControlException
我正在开发一个打印文件的 Java 小程序。 该小程序是“自签名的”。
打印功能是:
//argFilePath : path to file (http://localhost/Teste/pdf1.pdf)
//argPrintService : something like PrintServiceLookup.lookupDefaultPrintService()
private int print(String argFilePath, PrintService argPrintService){
try
{
DocPrintJob printJob = argPrintService.createPrintJob();
Doc doc;
DocAttributeSet docAttrSet = new HashDocAttributeSet();
PrintRequestAttributeSet printReqAttr = new HashPrintRequestAttributeSet();
URL url = new URL(argFilePath);
doc = new SimpleDoc(url.openStream(), DocFlavor.INPUT_STREAM.AUTOSENSE, docAttrSet);
printJob.print(doc, printReqAttr);
} catch (Exception e) {
System.out.println(e);
return 1;
}
return 0;
}
尝试打开文件时出现此异常:
java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:80 connect,resolve)
HTML/JavaScrip 的
<input onclick="alert(document.getElementById('xpto').print('http://localhost/Teste/pdf1.pdf'));" type="button"/>
<applet width="180" height="120" code="printers.class" id="xpto" archive="printerAPI.jar"></applet>
使用正确:
DocFlavor.INPUT_STREAM.AUTOSENSE
这个想法似乎是打印尽可能多的文件类型 - pdf、docx、jpg 等。
您如何解决例外吗?
I'm working on an Java applet that prints a file.
The applet is "self-signed".
The print function is:
//argFilePath : path to file (http://localhost/Teste/pdf1.pdf)
//argPrintService : something like PrintServiceLookup.lookupDefaultPrintService()
private int print(String argFilePath, PrintService argPrintService){
try
{
DocPrintJob printJob = argPrintService.createPrintJob();
Doc doc;
DocAttributeSet docAttrSet = new HashDocAttributeSet();
PrintRequestAttributeSet printReqAttr = new HashPrintRequestAttributeSet();
URL url = new URL(argFilePath);
doc = new SimpleDoc(url.openStream(), DocFlavor.INPUT_STREAM.AUTOSENSE, docAttrSet);
printJob.print(doc, printReqAttr);
} catch (Exception e) {
System.out.println(e);
return 1;
}
return 0;
}
I get this exception when trying to open the file:
java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:80 connect,resolve)
HTML/JavaScrip
<input onclick="alert(document.getElementById('xpto').print('http://localhost/Teste/pdf1.pdf'));" type="button"/>
<applet width="180" height="120" code="printers.class" id="xpto" archive="printerAPI.jar"></applet>
is correct to use:
DocFlavor.INPUT_STREAM.AUTOSENSE
The idea seems to be to print as many file type as possible - pdf, docx, jpg, etc.
How can you fix the exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到了答案(在 stackoverflow 上,哈哈:D)!
看起来问题是:
"javascript 没有文件访问权限”,
因此该小程序被阻止。我们必须使用
doPrivileged
这是我的实现:
Found the answer (on stackoverflow lol :D)!
It looks like the problem was:
"javascript does not have file access permissions"
so the applet is blocked. we have to use
doPrivileged
Here is my implementation:
您可能了解到这一点:
因为小程序无法连接到除它们来源的网站之外的网站。现在,这是非常愚蠢的,因为人们会认为
localhost
不是另一个网站,但 Java SecurityManager 必须只查看 IP 地址。因此,如果浏览器连接到74.125.224.224
,那么 Java 小程序必须 连接到该地址,这与localhost
不同,后者的地址是127.0.0.1
。这只会解决套接字权限错误。但是,如果您尝试访问用户的硬件,您可能会遇到其他问题。在这种情况下,您需要制作一个证书,并且用户将选择是否运行您的小程序。
如果您只想在家庭计算机上运行它,那么您的主目录中需要一个纯文本
java.policy
文件。 (~/.java.policy 对于 Unix 用户。)在该文件中,您将键入:将此文件保存到主目录后,所有 java 小程序将被授予运行任何内容的完全权限。就像 SecurityManager 不存在一样,所以要小心一点。完成测试后,我建议删除此文件。
You probably got this:
because applets can't make connections to websites, other than the one they came from. Now, this is terribly silly because one would think
localhost
is not another website, but the Java SecurityManager must only look at IP address. Therefore, if the browser is connected to74.125.224.224
then the Java applet must connect to that address—which is different fromlocalhost
, whose address is127.0.0.1
.This will just take care of the Socket Permission error. But, you'll probably run into something else if you're trying to access the user's hardware. In which case, you'll need to make a certificate and the user will choose whether or not to run your applet.
If you just want to run this on your home computer then, you need a plain-text
java.policy
file in your home directory. (~/.java.policy for Unix people.) In that file you'll type:After you save this file in your home directory, all java applets will be given full permission to run—anything. It'll be like the SecurityManager doesn't exist, so try to be a bit careful. After you're done with testing, I'd recommend to delete this file.