j2ssh远程到远程文件传输
以下代码对于本地主机到远程服务器运行良好。但我的应用程序在单独的服务器上运行,因此我需要从 remote1
服务器中选取文件,然后将其放入 remote2
服务器中。我该如何使用 J2SSH 来做到这一点?
package com.test.sftp;
import java.io.File;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import com.sshtools.daemon.SshServer;
import com.sshtools.j2ssh.SftpClient;
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.configuration.SshConnectionProperties;
import com.sshtools.j2ssh.sftp.SftpFile;
import com.sshtools.j2ssh.transport.ConsoleKnownHostsKeyVerification;
import com.sshtools.j2ssh.transport.IgnoreHostKeyVerification;
import com.sshtools.j2ssh.transport.InvalidHostFileException;
import com.sshtools.j2ssh.transport.publickey.SshPublicKey;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
public class SftpTest {
/**
* @param args
*/
public String userName;
public String uri;
public Date date;
public String sdate;
public DateFormat formatter ;
public String serviceStart;
public String hostname=null;
public String username=null;
public String password=null;
public String hostname1=null;
public String username1=null;
public String password1=null;
public String remotepath=null;
public String localpath=null;
public String remoteFilename=null;
public void con()
{
SshClient ssh=null;
SftpClient sftp=null;
SftpFile sstp = null;
try
{
hostname="192.168.1.1";
username="xxxxx";
password="xxxxxxx";
SshClient client = new SshClient();
SshConnectionProperties connectionProperties = new SshConnectionProperties();
connectionProperties.setTransportProvider(SshConnectionProperties.USE_STANDARD_SOCKET); // or USE_STANDARD_SOCKET or USE_SOCKS4_PROXY or USE_SOCKS5_PROXY
connectionProperties.setProxyHost("proxy.example.com");
connectionProperties.setProxyPort(22);
connectionProperties.setHost(hostname);
connectionProperties.setPort(22);
client.connect(connectionProperties, new IgnoreHostKeyVerification());
PasswordAuthenticationClient authenticationClient = new PasswordAuthenticationClient();
authenticationClient.setUsername(username);
authenticationClient.setPassword(password);
int result = client.authenticate(authenticationClient);
System.out.println("result value ::"+result);
if (result == AuthenticationProtocolState.COMPLETE)
System.out.println("success Authentication");
else
System.out.println("failed Authentication");
System.out.println(client.isConnected());
SftpClient sftpClient = client.openSftpClient();
String localpath = "C:/Documents and Settings/user1/Desktop/images";
sftpClient.lcd(localpath);
//sftpClient.cd("/");
File folder = new File("C:/Documents and Settings/user/Desktop/images");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
String str ="C:/Documents and Settings/user1/Desktop/images/"+listOfFiles[i].getName();
// sftpClient.put(str,"/usr/project/images/");
System.out.println("File " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
sftpClient.quit();
client.disconnect();
}
catch(Exception e)
{
System.out.println("Exception while connecting to the remote server" + e);
}
}
public static void main(String args[]) throws Exception
{
SftpTest obj = new SftpTest();
obj.con();// calling the function
}
}
请帮助我了解如何实现这一目标。谢谢。
The following code is working fine for localhost to remote server. But my application runs on a separate server so I need to pick files from remote1
server and then put it in remote2
server. How would I do this with J2SSH?
package com.test.sftp;
import java.io.File;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import com.sshtools.daemon.SshServer;
import com.sshtools.j2ssh.SftpClient;
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.configuration.SshConnectionProperties;
import com.sshtools.j2ssh.sftp.SftpFile;
import com.sshtools.j2ssh.transport.ConsoleKnownHostsKeyVerification;
import com.sshtools.j2ssh.transport.IgnoreHostKeyVerification;
import com.sshtools.j2ssh.transport.InvalidHostFileException;
import com.sshtools.j2ssh.transport.publickey.SshPublicKey;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
public class SftpTest {
/**
* @param args
*/
public String userName;
public String uri;
public Date date;
public String sdate;
public DateFormat formatter ;
public String serviceStart;
public String hostname=null;
public String username=null;
public String password=null;
public String hostname1=null;
public String username1=null;
public String password1=null;
public String remotepath=null;
public String localpath=null;
public String remoteFilename=null;
public void con()
{
SshClient ssh=null;
SftpClient sftp=null;
SftpFile sstp = null;
try
{
hostname="192.168.1.1";
username="xxxxx";
password="xxxxxxx";
SshClient client = new SshClient();
SshConnectionProperties connectionProperties = new SshConnectionProperties();
connectionProperties.setTransportProvider(SshConnectionProperties.USE_STANDARD_SOCKET); // or USE_STANDARD_SOCKET or USE_SOCKS4_PROXY or USE_SOCKS5_PROXY
connectionProperties.setProxyHost("proxy.example.com");
connectionProperties.setProxyPort(22);
connectionProperties.setHost(hostname);
connectionProperties.setPort(22);
client.connect(connectionProperties, new IgnoreHostKeyVerification());
PasswordAuthenticationClient authenticationClient = new PasswordAuthenticationClient();
authenticationClient.setUsername(username);
authenticationClient.setPassword(password);
int result = client.authenticate(authenticationClient);
System.out.println("result value ::"+result);
if (result == AuthenticationProtocolState.COMPLETE)
System.out.println("success Authentication");
else
System.out.println("failed Authentication");
System.out.println(client.isConnected());
SftpClient sftpClient = client.openSftpClient();
String localpath = "C:/Documents and Settings/user1/Desktop/images";
sftpClient.lcd(localpath);
//sftpClient.cd("/");
File folder = new File("C:/Documents and Settings/user/Desktop/images");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
String str ="C:/Documents and Settings/user1/Desktop/images/"+listOfFiles[i].getName();
// sftpClient.put(str,"/usr/project/images/");
System.out.println("File " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
sftpClient.quit();
client.disconnect();
}
catch(Exception e)
{
System.out.println("Exception while connecting to the remote server" + e);
}
}
public static void main(String args[]) throws Exception
{
SftpTest obj = new SftpTest();
obj.con();// calling the function
}
}
Please help me understand how I can achieve this. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定我是否理解你的问题...
如果你只能通过 SFTP 访问两台服务器,并且你的代码在本地运行(在客户端上),那么你唯一的选择就是下载文件并将其上传到另一台服务器。如果您可以通过 SSH 访问 server1,那么您可以创建一个脚本,用于将文件上传到服务器 2,或者从服务器 2 下载文件。
如果您的代码在 server1 上运行,并且您需要将文件从 server1 上传到 server2,那么该怎么做呢?与你当前从client1上传代码到server2时的情况不同(除了本地路径不同)?
Not sure that I understood your question ...
If you only have SFTP access to both servers, and your code runs locally (on the client), then your only option is to download the file and upload it to another server. If you have SSH access to server1, then you can create a script which will upload/download file to/from server 2.
And if your code is running on server1 and you need to upload the file from server1 to server2, then how is it different (other than the local path is different) from your current situation when you upload the code from client1 to server2?