Java - IOException:无法打开文件:访问被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11284792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:31:41  来源:igfitidea点击:

Java - IOException: Failed to open file: Access denied

javaioexception

提问by user1494618

I've been developing some software in Java that'll be distributed for use with Windows or MacOS. I'm programming on an iMac at the moment (because of the Apple specific requirements). The following method drags a helpfile (.pdf) out of resources and copies it locally so that it can be displayed on the local machine.

我一直在用 Java 开发一些软件,这些软件将分发给 Windows 或 MacOS 使用。我目前正在 iMac 上编程(因为 Apple 的特定要求)。下面的方法从资源中拖出一个帮助文件(.pdf)并复制到本地,以便在本地机器上显示。

private void mHelpActionPerformed(java.awt.event.ActionEvent evt) {                                      
    String sourceFile = "resources/BRC2Help.pdf";
    String destFile = "brc2help.pdf";
    File f = new File (destFile);
    if (!f.exists()) {
            try {
            URL url = ClassLoader.getSystemResource(sourceFile) ;
            InputStream is = url.openStream();
            OutputStream os = new FileOutputStream(destFile);
            byte[] b = new byte[2048];
            int length;
            while ((length = is.read(b)) != -1) {
                os.write(b, 0, length);
            }
            is.close();
            os.close();
            System.out.printf("loaded pdf file from resources: %d bytes\n",f.length());
            } catch (IOException ex) {System.out.printf("loadPDF: %s\n",ex);}
    } else {
        System.out.printf("%s exists: %d bytes\n",destFile,f.length());
    }

    try {
        if (f.exists()) {
                f.setReadable(true);
                if (Desktop.isDesktopSupported()) {
                Desktop.getDesktop().open(f);
            } else {
                System.out.println("Awt Desktop is not supported!");
            }
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this,
                String.format("%s\n",ex),
                "Helpfile Problem?",
                JOptionPane.ERROR_MESSAGE);
    }
}                                     

This all works well enough on the Mac but gives an IOExceptionon Windows 7:

这一切在 Mac 上都运行良好,但IOException在 Windows 7 上却是:

java.io.IOException: Failed to open file:/C:/Program%20Files/BRC2/brc2help.pdf. Error message: Access denied.

java.io.IOException: Failed to open file:/C:/Program%20Files/BRC2/brc2help.pdf. Error message: Access denied.

Any idea what's going wrong here ? I've tried copying the .pdf file to other locations but the result is the same.

知道这里出了什么问题吗?我试过将 .pdf 文件复制到其他位置,但结果是一样的。

回答by HeatfanJohn

What is the working directory for the program when running on Windows? It is possible that the user context running the program doesn't have rights to write to c:\Program Files\.

在 Windows 上运行时程序的工作目录是什么?运行程序的用户上下文可能无权写入c:\Program Files\.

You did not specify the path to the file so my assumption is that c:\program files\brc\is the working directory while running the program. Since Windows Vista, you need to have full administration rights to be able to write to the Program Filesand other directories.

您没有指定文件的路径,所以我的假设是c:\program files\brc\运行程序时的工作目录。从 Windows Vista 开始,您需要拥有完整的管理权限才能写入Program Files和其他目录。

Updated: 7/1/2012

更新时间:2012 年 7 月 1 日

Ok, I was able to stub out your program and run it as a main class and was able to have Desktop.getDesktop().open(f);open a PDF file on Windows 7. I manually put a PDF file into the bin directory where Eclipse compiled my class file to.

好的,我能够将您的程序存根并将其作为主类运行,并且能够Desktop.getDesktop().open(f);在 Windows 7 上打开 PDF 文件。我手动将 PDF 文件放入 Eclipse 将我的类文件编译到的 bin 目录中。

I now need to try moving the class file and pdf into a subdirectory of c:\program files\and see if I get the access denied exception you receive.

我现在需要尝试将类文件和 pdf 移动到 的子目录中c:\program files\,看看我是否收到您收到的拒绝访问异常。

Hum, I was able to get Desktop.getDesktop().open(f);open the PDF file when it was in c:\program files\test\, see my output below:

嗯,我可以Desktop.getDesktop().open(f);在 PDF 文件中打开它c:\program files\test\,请参阅下面的输出:

C:\Program Files\test>"\Program Files (x86)\Java\jre7\bin\java.exe" MyTestClass
brc2help.pdf exists: 943123 bytes

C:\Program Files\test>dir
 Volume in drive C is OS
 Volume Serial Number is 2035-793F

 Directory of C:\Program Files\test

07/01/2012  10:25 PM    <DIR>          .
07/01/2012  10:25 PM    <DIR>          ..
07/01/2012  09:55 PM           943,123 BRC2Help.pdf
07/01/2012  09:57 PM             2,391 MyTestClass.class
               2 File(s)        945,514 bytes
               2 Dir(s)  567,516,254,208 bytes free

What JRE are you using? Mine is java version "1.7.0_01" Java(TM) SE Runtime Environment (build 1.7.0_01-b08)

你用的是什么JRE?我的是java version "1.7.0_01" Java(TM) SE Runtime Environment (build 1.7.0_01-b08)

回答by saada

I agree with HeatfanJohn, it is definitely a permission issue.

我同意 HeatfanJohn 的观点,这绝对是一个许可问题。

Try compiling your java code as Administrator from the command line. Simply run cmd.exe as Administrator and then try compiling/running again.

尝试从命令行以管理员身份编译 Java 代码。只需以管理员身份运行 cmd.exe,然后再次尝试编译/运行。

回答by Pavel Sedek

I'm experiencing the same issue. Don't know the reason, but found a solution.

我遇到了同样的问题。不知道原因,但找到了解决方案。

Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL \"" + StringEscapeUtils.escapeJava(f.getAbsolutePath()) + "\"");

you should use this only if the Desktop method fails, because this is only for Windows.

只有在桌面方法失败时才应该使用它,因为这仅适用于 Windows。