有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java NIO将文件移动到共享位置

在我的情况下,我需要将文件从本地文件夹复制到共享位置

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不允许在路径前面使用slashes \,如本question中所述。在我的例子中,我必须将共享位置指定为以slashes开头的url。我如何克服这个问题

有没有办法将文件从本地位置复制到共享位置


共 (1) 个答案

  1. # 1 楼答案

    用于UNC路径的Java字符串值为:

    \\\10.101.1.2\\resources\\Files\\exbury\\Tulips.jpg

    UNC路径通常采用以下形式:

    \\10.101.1.2\resources\Files\exbury\Tulips.jpg  
    

    每个斜杠\必须在Java字符串中转义为\\

    结果路径的Java字符串值应为:

    \\\\10.101.1.2\\resources\\Files\\exbury\\Tulips.jpg

    您缺少前面的\字符

    使用/也有效,不需要转义;使用/的Java字符串值是:

    //10.101.1.2/resources/Files/exbury/Tulips.jpg