获取“java.nio.file.AccessDeniedException"尝试写入文件夹时 [英] Getting "java.nio.file.AccessDeniedException" when trying to write to a folder

查看:86
本文介绍了获取“java.nio.file.AccessDeniedException"尝试写入文件夹时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,每次我尝试使用 Tomcat 上的 java webapp 写入我计算机上的文件夹时,我都会收到 java.nio.file.AccessDeniedException.此文件夹的权限设置为对我计算机 (Windows) 上的所有人进行完全控制.有人知道我为什么会收到此异常吗?

For some reason I keep getting java.nio.file.AccessDeniedException every time I try to write to a folder on my computer using a java webapp on Tomcat. This folder has permissions set to full control for everyone on my computer (Windows). Does anybody know why I get this exception?

这是我的代码:

public void saveDocument(String name, String siteID, byte doc[]) {
    try {
        Path path = Paths.get(rootDirectory + siteID);
        if (Files.exists(path)) {
            System.out.println("Exists: " + path.toString());
            Files.write(path, doc);
        } else {
            System.out.println("DOesn't exist");
            throw new Exception("Directory for Site with ID " + siteID + "doesn't exist");
        }
    } catch (FileSystemException e) {
        System.out.println("Exception: " + e);
        e.printStackTrace();
    } catch (IOException e ) {
        System.out.println("Exception: " + e);
        e.printStackTrace();
    } catch (Exception e) {
        System.out.println("Exception: " + e);
        e.printStackTrace();
    }

这里是错误:

异常:java.nio.file.AccessDeniedException:C:safesite_documentssite1java.nio.file.AccessDeniedException: C:safesite_documentssite1在 sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)在 sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)在 sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)在 sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)在 java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:430)在 java.nio.file.Files.newOutputStream(Files.java:172)在 java.nio.file.Files.write(Files.java:3092)

Exception: java.nio.file.AccessDeniedException: C:safesite_documentssite1 java.nio.file.AccessDeniedException: C:safesite_documentssite1 at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102) at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230) at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:430) at java.nio.file.Files.newOutputStream(Files.java:172) at java.nio.file.Files.write(Files.java:3092)

可能的原因:请参阅我在超级用户上的帖子,了解我如何无法取消选中 Windows 7 上任何文件夹的只读".即使所有文件夹都不是只读的,但 Java 除外.

推荐答案

好吧,事实证明我在做一些愚蠢的事情.我没有将新文件名附加到路径中.

Ok it turns out I was doing something stupid. I hadn't appended the new file name to the path.

我有

rootDirectory = "C:\safesite_documents"

但应该是

rootDirectory = "C:\safesite_documents\newFile.jpg" 

抱歉,这是一个愚蠢的错误.

Sorry it was a stupid mistake as always.

这篇关于获取“java.nio.file.AccessDeniedException"尝试写入文件夹时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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