gpt4 book ai didi

Java 小程序 java.security.AccessControlException

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:26:19 25 4
gpt4 key购买 nike

我正在开发一个打印文件的 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 等。

如何修复异常?

最佳答案

找到答案(在 stackoverflow 上 lol :D)!

看起来问题是:

"javascript does not have file access permissions"

所以小程序被屏蔽了。我们必须使用

AccessController.doPrivileged()

doPrivileged

这是我的实现:

private int print(String argFilePath, PrintService argPrintService){
cPrint cP = new cPrint(argFilePath, argPrintService);
return (Integer) AccessController.doPrivileged(cP);
}

class cPrint implements PrivilegedAction<Object> {
String FilePath;
PrintService PrintService;

public cPrint(String argFilePath, PrintService argPrintService) {

this.FilePath = argFilePath;
this.PrintService = argPrintService;

};
public Object run() {
// privileged code goes here

try
{

DocPrintJob printJob = PrintService.createPrintJob();
Doc doc;
DocAttributeSet docAttrSet = new HashDocAttributeSet();
PrintRequestAttributeSet printReqAttr = new HashPrintRequestAttributeSet();



URL url = new URL(FilePath);
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 小程序 java.security.AccessControlException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5439568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com