简体   繁体   English

在Linux上运行的Java代码中,如何指向共享的Windows文件夹?

[英]In Java code running on Linux, how to point to a shared Windows folder?

I have a shared Windows folder: 我有一个共享的Windows文件夹:

\\servername.domain.com\\dir\\warname \\ servername.domain.com \\ dir \\ warname

This is how I access it from Java code running on Windows OS: 这是我从Windows OS上运行的Java代码访问它的方式:

// \dir
String directory = getInitParameter ("directory");

// /warname
final String contextPath = this.getServletContext().getContextPath(); 

// \dir/warname
directory += (directory.endsWith("/") ? contextPath.substring(1) : contextPath); 

// //servername.domain.com/\dir/warname

directory = "//" + getDatabaseServerName() + (directory.startsWith("/") ? "" : "/") + directory;

// \\servername.domain.com\dir\warname
File shareDir = new File(directory);

if (!shareDir.exists()) {
        if (!shareDir.mkdirs()) { 
            throw new Exception ("Error: " + shareDir + " does not exist and could not be created.");
        }
    }

From this point forward, I can access the \\servername.domain.com\\dir\\warname and write to it. 从现在开始,我可以访问\\ servername.domain.com \\ dir \\ warname并对其进行写入。

If this code runs on Linux server, this is what I get: 如果此代码在Linux服务器上运行,这就是我得到的:

directory: //servername.domain.com/\dir/warname

shareDir: /severname.domain.com/\dir/warname

And then the above exp. 然后上面的exp。 will be thrown: 将被抛出:

java.lang.Exception: Error: /servername.domain.com/\dir/warname does not exist and could not be created.

So it tries to create a new dir and it fails. 因此,它尝试创建一个新的目录,但失败了。

How can I point to the same shared Windows folder from Linux? 如何从Linux指向相同的共享Windows文件夹?

I did Google search it but was not able to find the solution. 我做了Google搜索,但找不到解决方案。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

You may need to install the SMB client on Linux. 您可能需要在Linux上安装SMB客户端。 See this article on how to do that: http://www.howtogeek.com/176471/how-to-share-files-between-windows-and-linux/ 请参阅有关此操作的文章: http : //www.howtogeek.com/176471/how-to-share-files-between-windows-and-linux/

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

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