我已经在Tomcat Server
(v8)中部署了 webApplication ,该服务器在Windows 2008 R2
PC上运行服务。在此 webApplication 中,用户使用表单上传文件(照片)。我想要将这些照片存储在网络中的NAS
磁盘中(服务器和NAS磁盘都在同一网络中)。
我阅读并关注了相关的answer。我首先尝试将照片保存在服务器C:
的本地磁盘中,并且可以正常工作!
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();
}
}
但是,如果我将位置路径更改为NAS
磁盘
String pathRoot = "\\\\192.168.0.7\\Share\\photos\\";
然后出现以下错误:
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)
为了避免出现上述错误,如何添加网络磁盘的凭据?
编辑1
我从Windows映射了网络磁盘,并且可以访问该文件夹。但是,该错误仍然存在。
答案 0 :(得分:0)
您可以通过两种方式实现这一目标:
1)由于网络驱动器中的“登录”是由Windows完成的,因此请转到您的服务器(安装并运行了Tomcat的服务器)上,访问共享文件夹\\192.168.0.7\Share\photos
,并在其要求提供凭据时将其放置并在Windows上选中复选框记住凭据进行保存。然后,您可以在控制面板上编辑管理网络密码或从中删除。在here中查找更多信息
2)您必须为Java使用外部库。有一个有用的答案here
希望有帮助
答案 1 :(得分:0)
您可以使用Runtime.exec执行net use \\192.168.0.7\Share\photos your-password /user:your-domain\your-user-name /persistent:yes /savecred
。在Web应用程序初始化期间只需要执行一次。