简体   繁体   English

通过Windows env中运行的Java程序读取Linux服务器上的文件

[英]Read a file on Linux server through java program running in Windows env

I have simple Java program which reads a file and writes it on my console in eclipse tool. 我有一个简单的Java程序,可以读取文件并将其写入Eclipse工具的控制台中。 I'm 我是
trying to execute the same java program to read the file on Remote Linux server. 尝试执行相同的Java程序以读取远程Linux服务器上的文件。 Please help how can I achieve it? 请帮助我如何实现它?

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class Test {     
public static void main(String[] args){
    String path = "C:/tmp"; 
    String file = "java2502201411.txt"; 
      try
      {
      FileInputStream in = new FileInputStream(path + "/" + file);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine; 
      while((strLine = br.readLine())!= null)
      {
       System.out.println(strLine);
      }
      }catch(Exception e){
       System.out.println(e);
      } 
   }  
}

I'm able to call a file from other windows environment which is in network. 我可以从网络中的其他Windows环境中调用文件。 for Eg: I'm trying to read a file on my windows network such as usmnp1804 terminal. 例如:我正在尝试在Windows网络上读取文件,例如usmnp1804终端。 String path = "//usmnp1804/C$/tmp". 字符串路径=“ // usmnp1804 / C $ / tmp”。 In the similar way I'm trying to read a file on Linux server but it is not working as intended, getting an error such as File not found Exception. 以类似的方式,我尝试在Linux服务器上读取文件,但该文件未按预期工作,并出现诸如File not found Exception之类的错误。 I suspect such as my java program is not able to hit Linux server path. 我怀疑诸如我的Java程序无法打Linux服务器路径。 Please help. 请帮忙。 String path = "/home/jctadm/tmp". 字符串路径=“ / home / jctadm / tmp”。

Thanks Raj 谢谢拉吉

Java can't natively open files shared across CIFS. Java无法本地打开跨CIFS共享的文件。 You have to use a client library. 您必须使用客户端库。 JCIFS seems to be the de facto standard for this. JCIFS似乎是事实上的标准。 Apache Commons Virtual File System also supports this. Apache Commons虚拟文件系统也支持此功能。 Both have tags here on SO: and . 两者都在SO上有标签:

This example might help, this is a basic example to use VFS to retrieve files from a remote system using the SFTP protocol. 该示例可能会有所帮助,这是使用VFS通过SFTP协议从远程系统检索文件的基本示例。 Files matching a specified regular expression are retrieved. 检索与指定正则表达式匹配的文件。

http://wiki.apache.org/commons/SimpleSftpFileDownload http://wiki.apache.org/commons/SimpleSftpFileDownload

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

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