java 使用java从远程位置读取文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30785155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:39:51  来源:igfitidea点击:

Read file from a remote location with java

java

提问by Skizzo

I have some doubts about the way to read file from a remote (inside our network) location with java. I'm developing a simple java application from a windows machine and I'm able to reach a remote file that resides on a unix machine in this way:

我对使用 java 从远程(在我们的网络内)位置读取文件的方式有一些疑问。我正在从 Windows 机器开发一个简单的 java 应用程序,我能够以这种方式访问​​驻留在 unix 机器上的远程文件:

File fileToRead=new File(new URI(file:////192.168.0.27/export/myFile.txt))

With the same application my collegue that is using kubuntu to develop is not able to reach the file. I get FileNotFoundException.

使用同一个应用程序,我使用 kubuntu 开发的同事无法访问该文件。我得到 FileNotFoundException。

What can be the problem?

可能是什么问题?

UPDATE 1I would want to use jcfis to solve my problem, but in this cas the application will work both on windows than on linux?

更新 1我想使用 jcfis 来解决我的问题,但在这个 cas 中,应用程序将在 Windows 上运行而不是在 linux 上运行?

采纳答案by Skizzo

I solved using a jcifslibrary in the following way

我通过以下方式解决了使用jcifs库的问题

SmbFile fileToRead= new SmbFile(smb://192.168.0.27/export/myFile.txt);

This works in both developing environment (Windows and Linux)

这适用于开发环境(Windows 和 Linux)

回答by AlphSpirit

EDIT: The OP has specified which protocol he would want to use. This answer fails to use that said protocol so it is not valid anymore.

编辑:OP 已经指定了他想要使用的协议。此答案未能使用该协议,因此不再有效。

Use a URL object instead:

改用 URL 对象:

URL url = new URL("http://someaddress.com/somefile.txt");

With this URL, you can open a InputStream:

使用此 URL,您可以打开 InputStream:

InputStream is = url.openStream();

Which you can read with a BufferedReader

您可以使用 BufferedReader 读取

BufferedReader br = new BufferedReader(new InputStreamReader(is));

EDIT: This will work fine assuming HTTP can be used to download the file.

编辑:假设可以使用 HTTP 下载文件,这将正常工作。

Full code:

完整代码:

URL url = new URL("http://someaddress.com");
InputStream is = url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
// You can read lines like this
String line = br.readLine();

回答by nom

If you are using FTP or HTTP you can directly use @AlphSpirit's answer. Otherwise you can use the java.net.Socketand implement your own protocol (which is easier than using FTP or HTTP, you will need a java.net.ServerSocketrunning on the other machine).

如果您使用的是 FTP 或 HTTP,您可以直接使用@AlphSpirit 的回答。否则你可以使用java.net.Socket并实现你自己的协议(这比使用 FTP 或 HTTP 更容易,你需要java.net.ServerSocket在另一台机器上运行)。

Edit:You said you would like to use JCFIS as it works both on Windows and Linux, but the JRE does too.

编辑:你说你想使用 JCFIS,因为它可以在 Windows 和 Linux 上运行,但 JRE 也可以。