Create a folder from Servlet and save file


Create a folder from Servlet and save file



In my form, user uploads a photo(s) and then I want to store that photo in a network disk (in our local network).



The following works fine, when I execute it from Netbeans in my PC:


public void saveFiles(List<Part> fileParts,String targetId) {
String pathRoot = "A:\ShareDisk1\Photos\";
String date = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());
try {
int counter = 1;
for (Part filePart : fileParts) {

String destPath = pathRoot + "\" + targetId;
Boolean flag = new File(destPath).mkdirs();
System.out.println("flag = " + flag);
File file = new File(destPath, targetId + " " + date + "-" + counter + ".jpg");

try (InputStream input = filePart.getInputStream()) {
Files.copy(input, file.toPath());
}

counter++;
}
} catch (IOException ex) {
ex.printStackTrace();
}
}



However, when I deploy the app in my Tomcat Server it doesn't work and I get a java.nio.file.NoSuchFileException exception:


java.nio.file.NoSuchFileException


java.nio.file.NoSuchFileException: A:ShareDisk1PhotosMachine 4Machine 4 20180702122146-1.jpg
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
at java.nio.file.spi.FileSystemProvider.newOutputStream(Unknown Source)
at java.nio.file.Files.newOutputStream(Unknown Source)
at java.nio.file.Files.copy(Unknown Source)



The path of the folder doesn't exist so I try to create it with


Boolean flag = new File(destPath).mkdirs();



which returns false. Reading the documentation:


false



Returns: true if and only if the directory was created, along with all
necessary parent directories; false otherwise



So, (if I understand the above info well) if the directory already exists, it will return false. However, in my case the directory doesn't exist, so the false what does mean?


false



In the Server (Windows Server 2008 R2) where the app is deployed, I have mapped the network disk in the letter A:. I tried also and created the folder by myself, to see if the folder exists what will happen but I still get the same error..


A:



Also, I check the JAVA version is the same in my PC and the server build 1.8.0_171.


JAVA


1.8.0_171



Edit 1



I tried also setting in web.xml the :


web.xml


<context-param>
<param-name>upload.location</param-name>
<param-value>A:ShareDisk1Photos</param-value>
</context-param>



And then using this:


String destPath = getServletContext().getInitParameter("upload.location") + "\" + targetId;



but I get the same result. When executing the app in Netbeans, I don't have any error, but when I deploy the app I submit the form I get the above error.



Edit 2



I tried to save the file in the local disk but I still get the same error. So the problem isn't the path of network location...



Edit 3



A quick recap: The above method works successfully when I run the webApp through NetBeans in my PC. However, when I deploy the webApp in a Windows Server PC (this is another PC, where TomCat Server runs as Windows Service) the above method throws the java.nio.file.NoSuchFileException` exception.


Windows Server


TomCat Server





this windows server with your app deployed is on the same network and the user running the webserver has write access to this directory? Maybe you have to create the output file before writing to an output stream for it?
– zack6849
Jul 2 at 11:13






Yes the folder exists. I have mapped the network disk in letter A: in the server and with full write/read permissions..
– yaylitzis
Jul 2 at 11:16





Try second recommendation, maybe Files.copy expects the destination path to exist first?
– zack6849
Jul 2 at 11:17





I tried also to create the folder before submit the form. But I get the same error
– yaylitzis
Jul 2 at 11:19





I mean the output file, not the folder
– zack6849
Jul 2 at 11:19









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Rothschild family

Cinema of Italy