I have deployed a webApplication in a Tomcat Server
(v8), which runs a service in a Windows 2008 R2
PC. In this webApplication, the user uses a form to upload files (photos). What I want is to store these photos in a NAS
disk in my network (both server and NAS disk is under the same network).
I read and followed this relevant answer. I tried firstly to save the photos in the local disk of my server C:
and it worked!
public void saveFiles(List<Part> fileParts,String targetId) {
String pathRoot = "C:\\Share\\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.err.println("mkdirs 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, if I change the location path to NAS
disk
String pathRoot = "\\\\192.168.0.7\\Share\\photos\\";
then I get the following error:
java.nio.file.FileSystemException: \\192.168.0.7\Share\photos\machines\4 20180704110827-1.jpg: Logon failure: unknown user name or bad password.
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)
at com.yaylitizis.production.Actions.saveFiles(Actions.java:1797)
at com.yaylitizis.production.Actions.processRequest(Actions.java:510)
at com.yaylitizis.production.Actions.doPost(Actions.java:1893)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.yaylitizis.production.FilterUTF8.doFilter(FilterUTF8.java:27)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
In order to avoid the above error, how I can add the credentials of the network disk?
Edit 1
I mapped the network disk from Windows and the folder is accessible. However, the error still exists..
You can achieve this in 2 ways:
1) Since the "login" in network drive is done by windows, go on your server (where tomcat is installed and running), access the shared folder
\\192.168.0.7\Share\photos
, and when it asks for credentials, put them and save with the checkbox remember credentials on Windows. You can then edit or remove from Manage your network password on control panel. Look here for more info2) You have to use external libraries for Java. There's an helpful answer about it here
Hope it helps