获取" java.nio.file.AccessDeniedException"当试图写入一个文件夹 [英] Getting "java.nio.file.AccessDeniedException" when trying to write to a folder

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

问题描述

由于某些原因,我每次尝试使用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(不存在);
抛出新的异常(带有ID的站点的目录+ siteID +不存在);

$ b $ 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_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)
在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)
位于java.nio.file.Files.newOutputStream(Files.java:172)
位于java.nio.file.Files.write(Files.java:3092 )

可能的原因:

< a href =https://superuser.com/questions/881348/cant-unset-read-only-only-applies-to-files-in-folder-in- windows-7>在超级用户上查看我的帖子,了解如何在Windows 7上无法取消选中只读文件夹。即使所有文件夹都不能读取除java以外的其他文件夹。

解决方案

好的,事实证明我是在做一些愚蠢的事情。我没有将新的文件名添加到路径中。
我有rootDirectory =C:\ safesite_documents,但它应该是rootDirectory =C:\safesite_documents\\\
ewFile.jpg。

<对不起,这是一个愚蠢的错误一如既往。


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). So I don't know why I would be getting this. Anyone know why?

Here's my code.

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

And here is the error.

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)

Possible reason why:

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.

解决方案

Ok it turns out I was doing something stupid. I hadn't appended the new file name to the path. I had "rootDirectory = "C:\safesite_documents" but it should have been 'rootDirectory = "C:\safesite_documents\newFile.jpg".

Sorry it was a stupid mistake as always.

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

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