简体   繁体   中英

Read file from remote machine without web server

I have a Spring Boot program that runs in a server, and it needs to read file from a different machine (Both machines are Windows OS). In the remote machine, I do not use any web-server such as apache/nginx - and I don't want to. I want to directly read files from the disk.

What I want is to provide the required params (probably IP, user name and password of the remote host), and a path in the file system - to direct access to the files without web server.

public void readFile(String ip, String userName, String password, String path);

How can I achieve this?

You need to do a scp (which allows copying files from different machines) from Java. This library will help

Also a working example which copies a file from remote to local

  JSch jsch = new JSch();
  Session session = jsch.getSession(user, host, 22);
  ...
  String command = "scp -f "+rfile;
  Channel channel = session.openChannel("exec");
  ((ChannelExec)channel).setCommand(command);

  InputStream in = channel.getInputStream();
  channel.connect();

  // "in" contains the input stream of the file

You can do something like

  1. activate FTP protocol on the other machine and use java ftp implementations
  2. create a network shared drive and grant access to specific windows users within your domain. you just need to know the network drive url and can access the file as if it was locally
  3. execute a seperate powershell /ssh / scp /... task from within your java code to open a remote-session and transfer the file
  4. write your own http-server application in java and run on it on the other pc and connect to it as a client
  5. ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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