系统相关
首页 > 系统相关> > java – FileUtils.copyFile()在目标是网络路径时不创建文件(在Windows上)

java – FileUtils.copyFile()在目标是网络路径时不创建文件(在Windows上)

作者:互联网

我正在使用apache common的FileUtils.copyFile()将本地磁盘上的文件复制到网络共享位置.共享文件夹已存在,运行该应用程序的用户具有该权限. FileUtils.copyFile()执行时没有异常.但是,该文件实际上并未创建.

File sourceFile = new File ("C:\\sourcefile.txt");
File destinationFile = new File("\\data-server\\my_share\\dest.txt");
// false
System.out.println("Before copy, file exists? " + destinationFile.exists());
FileUtils.copyFile(sourceFile, destinationFile);
// true
System.out.println("After copy, file exists? " + destinationFile.exists());

将网络共享路径指定为目标,它不起作用.但是,如果我在Windows中映射网络驱动器并通过网络映射写入它,它就可以工作.非常奇怪的是我在复制操作之后调用了file.exists(),并且java报告文件存在,但它没有显示出来.

我也尝试使用FIleUtils.copyFileToDirectory(),只是指定目标目录而不是文件名.当目的地是网络路径时,我遇到了同样的问题.

解决方法:

您的目的地需要其他转义字符.

"\\\\data-server\\my_share\\dest.txt"

标签:java,windows,apache-commons-io
来源: https://codeday.me/bug/20190829/1764722.html