简体   繁体   English

Java无法从Linux在Windows共享文件夹中创建文件

[英]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.

相关问题 Java Mail-如何在Linux上为共享文件夹创建链接并在Windows中访问该文件夹 - Java Mail - how to create a link for a shared folder on linux and access the same in Windows Java文件通过身份验证上传到Windows共享文件夹 - Java file upload to windows shared folder with authentication 在Linux上运行的Java代码中,如何指向共享的Windows文件夹? - In Java code running on Linux, how to point to a shared Windows folder? 如何使用Java和Jackcess从Linux机器访问共享文件夹中的远程.mdb文件 - How to access the remote .mdb file in shared folder using java and jackcess from linux machine 使用java代码从linux机器访问共享文件夹 - Access shared folder from linux machine using java code 使用java连接到Windows中的共享文件夹 - connecting to shared folder in windows with java 在Windows和Linux计算机上使用Java程序创建新文件夹 - Create a New folder using Java Program on Windows and Linux machines Java - 无法在具有完全写入权限的共享文件夹上创建文件 - Java - Unable to create a file on a shared folder with full write access 在Linux下,不会通过Java代码创建文件夹和文件,但适用于Windows - Folder and file are not getting created by Java code under Linux, but it works for Windows 如何在Windows和Linux上从Java读取文件 - How to read file on Windows and Linux from Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM