0 votes
1 view
in Java by (9.2k points)

Below is 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();

    }

This is the error I get when I try to write to a folder using a java webapp on Tomcat. 

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)

Can anyone tell me how to resolve this? 

1 Answer

0 votes
by (19k points)

You have to append the new file name to the path like below in the above code:

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

Interested in Java? Check out this Java tutorial by Intellipaat. 

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...