问题
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!!
回答1:
This is not how java.io.File
works. It works on the local disk file system only, not on network resources.
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:
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)
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.
回答2:
try:
URL url = new URL( "file:///192.168.2.103//CompartidaMatias//tablaEstudios.txt" );
File inFile = new File( url.getFile() );
来源:https://stackoverflow.com/questions/13884381/copying-files-from-a-shared-folder-to-a-local-folder-in-javaservlet