Java open file on shared location

a 夏天 提交于 2021-02-08 07:44:30

问题


I want to do something like this:

File root = new File("C:/file.txt");

but on a folder which is shared on a local network. So lets say the file is on 192.168.1.28 how make it with above command?

Next not working:

File root = new File("//192.168.1.2/file.txt");

File root = new File("\\\\192.168.1.2/file.txt");

File root = new File("\\192.168.1.2/file.txt");

File root = new File("file:\\192.168.1.2/file.txt");

File root = new File("file://192.168.1.2/file.txt");

Thanks a lot.


回答1:


I think it is possible. However you need to first mount the share using for example Samba - more info here - java read file from network device

Hope it helps




回答2:


You can use Apache Commons VFS. It is a library that allow you to manipulate files on various kind of filesystem, one of them perfectly suited for your need is the CIFS file system:

Provides access to the files on a CIFS server, such as a Samba server, or a Windows share.

URI Format

smb://[ username[: password]@] hostname[: port][ absolute-path]

Examples

smb://somehost/home

The provider for CIFS file system is still in development, but you can give it a try. I've already used the library to give transparent access to files via http and ftp protocols.




回答3:


Java alone doesn't have support for networked file sharing, as the code you're supplying is trying to do.

But if you use a library, like Samba, then you can. But it would be different than the code you showed.

java read file from network device




回答4:


Java just don't support networked file sharing




回答5:


With Java 1.8, you can use Java to access files on shared location. Let's say you want to access a .xls file called (Sample.xls) on a shared location.

String location = "\\\your.shared.location.company.com\\folder1\\folder2\\";
String fileName = "Sample.xls";
FileInputStream fis = new FileInputStream(location + fileName);


来源:https://stackoverflow.com/questions/10356849/java-open-file-on-shared-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!