将文件从共享文件夹复制到JavaServlet中的本地文件夹 [英] Copying files from a shared folder to a local folder in javaservlet
问题描述
我正在开发servlet,我必须将文件(* .doc)从另一台计算机上的共享文件夹复制到servlet webapp空间,但是我不能.问题不是写在我的Apache服务器上,而是我期望从远程文件夹(LAN中的共享文件夹)复制文件时遇到麻烦.有什么建议或想法吗?
Im developing a servlet, and I have to copy a file (*.doc) from a shared folder in other computer to my servlet webapp space, but I can't. The trouble is not writing on my Apache Server, instead of, Im expecting troubles copying the file from the remote folder (shared folder in a LAN). Any suggest or idea?
File inFile = new File( "\\\\192.168.2.103\\CompartidaMatias\\tablaEstudios.txt");
out.println("<p> AbsolutePath --> " + inFile.getAbsolutePath() + "</p>");
out.println("<p> Path --> " + inFile.getPath() + "</p>");
out.println("<p> Nombre --> " + inFile.getName() + "</p>");
out.println("<p> WEBAPP_ROOT --> " + WEBAPP_ROOT + "</p>");
File outFile = new File(WEBAPP_ROOT + "mydoc3a.txt");
if (inFile.exists())
out.println("<p>FILE FOUND</p>");
else
out.println("<p>FILE NOT FOUND</p>");
我总是找不到文件:( 感谢您的时间伙伴!!我希望它可以解决,但是我花了我所有的想法.再次感谢!
I get always FILE NOT FOUND :( Thanks for your time buddies!! I hope it could be solved, but I have spent all my ideas. Thanks again!!
推荐答案
这不是java.io.File
的工作方式.它仅适用于本地磁盘文件系统,不适用于网络资源.
This is not how java.io.File
works. It works on the local disk file system only, not on network resources.
最好的选择是让您的操作系统平台创建一个指向网络资源的本地映射(虚拟磁盘的种类),并且,如果您使用的是Windows,则还要为其分配一个磁盘字母.这是 Microsoft Windows 7指南关于这个主题:
Your best bet is to let your operating system platform create a local mapping (kind of a virtual disk) pointing to the network resource and, given you're on Windows, assign it a disk letter as well. Here's a Microsoft Windows 7 guide on the subject:
您只需要将\\192.168.2.103
映射到例如Z:\
.完成此操作后,您应该能够按照以下方式找到文件:
You just have to map \\192.168.2.103
to e.g. Z:\
. Once done that, you should be able to locate the file as follows:
new File("Z:/CompartidaMatias/tablaEstudios.txt");
(请注意,/
的效果与\\
相同,并且使您免于转义)
(note that /
works as good as \\
and saves you from effort of escaping them)
请注意,此问题与servlet完全无关.这只是一个基本的Java问题.当在纯Java应用程序中使用main()
方法执行此操作时,您将遇到完全相同的问题(顺便说一句,它比Servlet允许更快,更轻松的测试).请记住这一点,以备您将来遇到的问题.
Note that this problem has completely nothing to do with servlets. It's just a basic Java problem. You'd have exactly the same problem when executing this in a plain Java application with a main()
method (which by the way allows for so much faster and easier testing than a servlet). Keep this in mind for your future questions.
这篇关于将文件从共享文件夹复制到JavaServlet中的本地文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!