简体   繁体   English

从UNIX写入Windows网络共享

[英]Write to a windows network share from unix

The following code works like a charm in eclipse under windows: 以下代码在Windows下的Eclipse中就像一个魅力一样工作:

public static void main(String[] args) 
{
    try
    {
        String filePath = "\\\\myserver\\dir";
        String fileName = "myFile.txt";
        FileWriter myFileWriter = new FileWriter(filePath + File.separator + fileName); 
        BufferedWriter myBufferedWriter = new BufferedWriter(myFileWriter);
        myBufferedWriter.write("test");
        myBufferedWriter.close();       
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

Now I want to run this code from a unix machine in the same network. 现在,我想从同一网络中的Unix机器上运行此代码。 The program runs, but does not write my file or throws an exception. 该程序可以运行,但不会写入我的文件或引发异常。 Any ides ? 有想法吗?

Cheers 干杯

If that destination unix machine has Samba installed you might want to try the following library: 如果该目标Unix机器安装了Samba,则可能需要尝试以下库:

http://jcifs.samba.org/ http://jcifs.samba.org/

You would need a username and password though. 但是,您将需要一个用户名和密码。

try {
        String filePath = "myserver/dir";
        String fileName = "myFile.txt";
        String user = "username";
        String password = "password";
        // URL: smb://user:passwd@host/share/filname
        SmbFileOutputStream out = new SmbFileOutputStream("smb://" + user + ":" + password + "@" + filePath
                + File.separator + fileName);
        out.write("test".getBytes());
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

This would also work with a windows machine as the destination if the server is configured as an SMB server. 如果服务器配置为SMB服务器,则也可以将Windows计算机作为目标服务器。

Because in Unix/Linux this is not the right path 因为在Unix / Linux中这不是正确的路径

String filePath = "\\\\myserver\\dir";

I suggest to check such path exist, and 99% chances you will not have permission to create them. 我建议检查这种路径是否存在,并且有99%的机会您将没有权限创建它们。 It would be more or less 或多或少

String filePath = "/usr/xx/"; 字符串filePath =“ / usr / xx /”;

Creating folder: 创建文件夹:

File temp = new File("temp");
boolean test = temp.mkDir();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM