[英]How to open a path in remote machine and write a file in that path using java
Based on user input data and database data, i need to create a file and place it in remote machine. 基于用户输入数据和数据库数据,我需要创建一个文件并将其放置在远程计算机中。 So the better way i could think of was connect to remote machine and write the file directly there.
因此,我能想到的更好的方法是连接到远程计算机并直接将文件写入那里。 So far using
JSch
i connected to remote machine. 到目前为止,我已使用
JSch
连接到远程计算机。 but i have no idea how to write a file in a particular location (root/usr/path/)
in this path i need to write and place a file (ddr12213124.NEW or ddr12213124.CSV)
. 但是我不知道如何在此路径的特定位置
(root/usr/path/)
中写入文件,我需要写入并放置文件(ddr12213124.NEW or ddr12213124.CSV)
。
I have attached the code for connecting to remote machine 我已经附加了用于连接到远程计算机的代码
package com.trial.classes;
import java.io.InputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.UserInfo;
public class transferFile {
public static void main(String[] args) {
// TODO Auto-generated method stub
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession("ragesh", "10.0.0.1", 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("password");
session.connect();
System.out.println("Connected to session successfully");
Channel channel = session.openChannel("sftp");
channel.connect();
System.out.println("Connected to Channel successfully");
ChannelSftp sftpChannel = (ChannelSftp) channel;
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
}
}
}
Now i would like to create a file (ddr12213124.NEW or ddr12213124.CSV)
and place it in the path root/usr/path/
现在我想创建一个文件
(ddr12213124.NEW or ddr12213124.CSV)
并将其放置在路径root/usr/path/
I asked this question earlier but it was marked duplicate and i was asked to raise a new question. 我早些时候问过这个问题,但它被标记为重复,并被要求提出一个新问题。 This is not duplicate.
这不是重复的。 No appropriate answer was found so far in the link that was posted earlier.
到目前为止,在先前发布的链接中找不到合适的答案。
You can use ChannelSFTP#put method to write a file to remote directory. 您可以使用ChannelSFTP#put方法将文件写入远程目录。 there are several flavors of the put method and you can use any one as per your needs.
put方法有多种风格,您可以根据需要使用任意一种。 Here is a sample code to write a File from Local System to remote system:
以下是将文件从本地系统写入远程系统的示例代码:
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession("ragesh", "10.0.0.1", 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("password");
session.connect();
System.out.println("Connected to session successfully");
Channel channel = session.openChannel("sftp");
channel.connect();
System.out.println("Connected to Channel successfully");
ChannelSftp sftpChannel = (ChannelSftp) channel;
// this will read file with the name test.txt and write to remote directory
sftpChannel.cd("/root/usr/path");
File f = new File("test.txt");
sftpChannel.put(new FileInputStream(f), f.getName()); // here you can also change the target file name by replacing f.getName() with your choice of name
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.