简体   繁体   中英

java.security.AccessControlException: access denied (java.io.FilePermission file read)

Below code prompts for the input box for the file name and location. When I enter file://C:/test/abc.tiff then I am getting

java.security.AccessControlException: access denied (java.io.FilePermission \\c\test\abc.tiff read)

Code Snippet

CMBDocument document = evt.getDocument();
    String docSaveFileName = (String) docToURL.get(document);
     System.out.println("docSaveFileName :"+docSaveFileName);
            docSaveFileName = docSaveFileName.replaceAll("servlet", "annotate");
            System.out.println("modified docSaveFileName :"+docSaveFileName);
            File tempFile = null;
            try {
                if (evt.getSaveAsNew() || document.isModified()) {
                    if (evt.getSaveAsNew()) {
                        docSaveFileName =
                            JOptionPane.showInputDialog(myGenDocViewer,
                                "Enter the name of the file to save the document:");
                    }
                    if (docSaveFileName == null) { // user cancelled
                        return;
                    }

                    currStreamingDocServices.setPreferredFormats(
                        new String[] { document.getMimeType()});

                    if (document.getCanWrite()) {
                        URL url = new URL(docSaveFileName);
                        OutputStream out = null;
                        String protocol = url.getProtocol();
                        String host = url.getHost();
                        // Use FileOutputStream if this URI is for a local file.
                        if (protocol.equals("file") 
                            && (host == null || host.length() == 0 || host.equals("localhost"))) {
                            out = new FileOutputStream(new File(url.getPath()));
                        }

                        else {
                            URLConnection urlCon = url.openConnection();
                            urlCon.setDoInput(false);
                            urlCon.setDoOutput(true);
                            urlCon.setUseCaches(false); // Enable tunneling.
                            if (urlCon instanceof HttpURLConnection) {
                                HttpURLConnection httpCon = (HttpURLConnection) urlCon;
                                httpCon.setRequestMethod("PUT");
                            }
                            urlCon.setRequestProperty("Content-type",document.getWriteMimeType());
                            out = urlCon.getOutputStream();
                        }
                        document.write(out);
                        out.close();
                        document.setModified(false);
                        document.setNew(false);
                        myGenDocViewer.setDocName(document, docSaveFileName);

Can I do this without signing jar file?

Sure thing. The plug-in 2 JRE allows us to access the local file-system from a sand-boxed applet, using the JNLP API services. Here is a demo. of the file services .

That demo. is of a free-floating application, but for the same in an applet (without source code), see GIFanim .

What sort of application are you using this code from? If it's an applet, you're touching a sandboxing problem: applets cannot read or write files on the file system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM