gpt4 book ai didi

java - 尝试写入文件夹时出现 "java.nio.file.AccessDeniedException"

转载 作者:IT老高 更新时间:2023-10-28 20:33:40 37 4
gpt4 key购买 nike

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

这是我的代码:

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();
}

这是错误:

Exception: java.nio.file.AccessDeniedException: C:\safesite_documents\site1 java.nio.file.AccessDeniedException: C:\safesite_documents\site1 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)

可能的原因:See my post on supersuser about how I can't uncheck 'Read Only' for any of my folders on windows 7. Even though all the folders aren't read only to anything but java.

最佳答案

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

我有

rootDirectory = "C:\\safesite_documents"

但应该是的

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

对不起,这是一个愚蠢的错误。

关于java - 尝试写入文件夹时出现 "java.nio.file.AccessDeniedException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28670576/

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