Java 小程序 java.security.AccessControlException [英] Java applet java.security.AccessControlException

查看:33
本文介绍了Java 小程序 java.security.AccessControlException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个打印文件的 Java 小程序.小程序是自签名的".

I'm working on an Java applet that prints a file. The applet is "self-signed".

打印功能为:

//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

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

The idea seems to be to print as many file type as possible - pdf, docx, jpg, etc.

如何修复异常?

推荐答案

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

Found the answer (on stackoverflow lol :D)!

看起来问题是:

javascript 没有文件访问权限"

所以小程序被阻止了.我们必须使用

so the applet is blocked. we have to use

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆