[英]Java Not able to create a file in windows shared folder from Linux
I am trying to create a file in a shared folder using the below code. 我正在尝试使用以下代码在共享文件夹中创建文件。 I am able to do it when i run this code on windows.
当我在Windows上运行此代码时,我能够做到。 But however when i run the same code on linux it is not working.
但是,当我在linux上运行相同的代码时,它不起作用。
In liunx it is creating a file named "\\192.168.1.102\\share\\1.pdf" in the folder where i run this java code instead of creating a file 1.pdf in the shared folder "\\192.168.1.102\\share\\". 在liunx中,它将在我运行此Java代码的文件夹中创建一个名为“ \\ 192.168.1.102 \\ share \\ 1.pdf”的文件,而不是在共享文件夹“ \\ 192.168.1.102 \\ share \\”中创建文件1.pdf。 。
It seems like while running on Linux the server was not identifying the path as a shared location, Instead it reads that as it's local path. 似乎在Linux上运行时,服务器未将路径标识为共享位置,而是将其读取为本地路径。
Are there any other ways to create a file in the shared folder? 还有其他方法可以在共享文件夹中创建文件吗? Could anyone please help me in resolving this?
有人可以帮我解决这个问题吗?
public class Test {
public static void main(String args[]) {
String s1 ="\\\\192.168.1.102\\share";
try{
FileOutputStream fos = new FileOutputStream(s1+"\\1.pdf");
fos.write(("Testing Success").getBytes());
fos.close();
}
catch(Exception e){
e.printStackTrace();
System.out.println(e.toString());
}
File file = new File(s1);
System.out.println(file.exists());
}
}
Linux simply doesn't support \\\\ip\\folder
path syntax. Linux根本不支持
\\\\ip\\folder
path语法。
You have to mount you shared folder before use. 使用前,必须先挂载共享文件夹。
Check if you have enough permissions to write file on the shared folder. 检查您是否具有足够的权限在共享文件夹上写入文件。 Or try running Jar of your code as administrator.
或者尝试以管理员身份运行您的代码Jar。
(1) Please use java function File.separator instead of "\\" in file path to make it platform independent. (1)请使用Java函数File.separator代替文件路径中的“ \\”,以使其与平台无关。
As Windows support "\\\\" and linux supports "/". 由于Windows支持“ \\\\”,而Linux支持“ /”。
(2) Check you have permission to write on that directory by using chmod command. (2)使用chmod命令检查您是否有权在该目录上写入。
Assuming the file-system is properly mounted in the path you're using, the code won't work cross-platform because paths in Windows use \\
as a segment separator, while paths in Linux use /
. 假设文件系统已正确安装在您使用的路径中,则该代码将无法跨平台运行,因为Windows中的路径使用
\\
作为段分隔符,而Linux中的路径使用/
。
You should use cross-platform code to generate paths. 您应该使用跨平台代码生成路径。 The
File
class has a static String
member called separator
that will have the correct value for the platform. File
类具有称为separator
的静态String
成员,该成员将具有平台的正确值。
String myPath = File.separator + "home" + File.separator + "bob"
The above will produce \\home\\bob
in Windows, /home/bob
in Linux/OSX 上面将在Windows中生成
\\home\\bob
,在Linux / OSX中生成/home/bob
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.