就我而言,我需要将文件从本地文件夹复制到共享位置。
Files.copy(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Tulips.jpg").toPath(), new File("\\\10.101.1.2\\resources\\Files\\exbury\\Tulips.jpg").toPath(),
java.nio.file.StandardCopyOption.REPLACE_EXISTING);
java.nio.file.InvalidPathException: Illegal char <> at index 1: \.101.1.2\ZoneResources\File Share\burusoth\Tulips.jpg at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182) at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153) at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77) at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94) at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255) at java.io.File.toPath(File.java:2234) at com.zone.qv2.s2c.resultupload.TestClass.method(TestClass.java:31) at com.zone.qv2.s2c.resultupload.TestClass.main(TestClass.java:22)
这意味着 NIO 不允许在路径前面使用斜杠\
,如 question 中所述。 。就我而言,我必须将共享位置指定为以 slashes
开头的 url。我该如何克服这个问题?
有什么方法可以将文件从本地位置复制到共享位置吗?
最佳答案
您用于 UNC 路径的 Java 字符串值是:
\\\10.101.1.2\\resources\\Files\\exbury\\Tulips.jpg
UNC 路径通常采用以下形式:
\\10.101.1.2\resources\Files\exbury\Tulips.jpg
每个斜杠 \
必须在 Java 字符串中转义为 \\
。
结果路径的 Java String 值应该是:
\\\\10.101.1.2\\resources\\Files\\exbury\\Tulips.jpg
您缺少前面的 \
字符。
使用/
也可以,不需要转义;使用 /
的 Java String 值是:
//10.101.1.2/resources/Files/exbury/Tulips.jpg
关于Java NIO 将文件移动到共享位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32251608/