JAVA代码实现远程服务器的文件操作

1、 下载svnkit的jar包
网址:https://mvnrepository.com/artifact/org.tmatesoft.svnkit/svnkit
Jar名:svnkit-1.9.0-r10609-atlassian-hosted.jar和svnkit-1.8.12.jar

注:JAVA类中导入jar— com.trilead.ssh2.*
例:
import com.trilead.ssh2.Connection;
import com.trilead.ssh2.ConnectionInfo;
import com.trilead.ssh2.SCPClient;
import com.trilead.ssh2.SFTPv3Client;

2、 实例代码
//【1】建立连接—远程服务器IP和端口号
Connection con = new Connection(“192.168.1.1”, 22);
ConnectionInfo connect = con.connect();
boolean isAuthed = con.authenticateWithPassword(“用户名 “, “密码”);

//【2-1】建立SCP客户端,执行封装的方法
SCPClient scpClient = con.createSCPClient();
//从服务器获取文件
scpClient.get(“/home/test /a.txt”, “C:/test/b.txt”);
//将本地文件上传到服务器
scpClient.put(“C:/test/c.txt “, “/home /test/d.txt”);

//【2-2.1】开启会话,执行原生linux命令
Session session = con.openSession();
//服务器文件移动并改名
session.execCommand(“mv -f /home/test/a.txt /home/test2/b.txt”);
//从服务复制文件到本地(下载)并改名
session.execCommand(“scp root@192.168.1.1:/home/test/a.txt /home/test2/b.txt”);
//从本地复制文件到服务器(上传)并改名
session.execCommand(“scp /home/test2/b.txt root@192.168.1.1:/home/test/a.txt “);

//【2-2.2】显示执行命令后的信息
InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true) {
String line = br.readLine();
if (line == null) {
log.info(“远程服务器返回信息:空”);
break;
}
log.info(“远程服务器返回信息:” + line);
}
//获得退出状态
System.out.println(“ExitCode: ” + session.getExitStatus());
//关闭会话
session.close();

//【3】关闭连接
con.close();

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java代码示例,用于从远程服务器下载NFS文件: ```java import java.io.*; import java.net.InetAddress; import java.net.UnknownHostException; import com.sun.nio.file.ExtendedOpenOption; import com.sun.nio.file.SensitivityWatchEventModifier; import com.sun.nio.file.SynchronousFileChannel; import com.sun.nio.file.WatchEvent.Kind; import com.sun.nio.file.WatchKey; import com.sun.nio.file.WatchService; import sun.nio.fs.UnixChannelFactory; public class DownloadNFSFile { public static void main(String[] args) { String remoteFilePath = "/nfs/file.txt"; // 远程NFS文件路径 String localFilePath = "/local/file.txt"; // 本地保存路径 try { // 获取远程服务器地址 InetAddress remoteAddress = InetAddress.getByName("remote.server.com"); // 创建文件输入流 InputStream in = new NFSInputStream(remoteAddress, remoteFilePath); // 创建本地文件输出流 OutputStream out = new FileOutputStream(localFilePath); // 缓冲区大小 byte[] buffer = new byte[1024]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } // 关闭流 in.close(); out.close(); System.out.println("文件下载成功!"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } /** * 自定义NFSInputStream类,用于从远程NFS文件读取数据 */ class NFSInputStream extends InputStream { private SynchronousFileChannel channel; public NFSInputStream(InetAddress remoteAddress, String remoteFilePath) throws IOException { UnixChannelFactory factory = new UnixChannelFactory(); channel = factory.newAsynchronousFileChannel(remoteAddress, remoteFilePath, null, null, null); } @Override public int read() throws IOException { return 0; } @Override public int read(byte[] b, int off, int len) throws IOException { return channel.read(ByteBuffer.wrap(b, off, len), 0).get(); } @Override public void close() throws IOException { channel.close(); } } ``` 需要注意的是,上述代码使用了Java 7中的NIO.2 API,因此需要在Java 7或更高版本上运行。同时,需要添加NFS客户端软件包的依赖。在Linux系统上,可以安装`nfs-common`软件包来提供NFS客户端支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值