- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们正在 Linux 服务器上部署 Java 项目。项目生成一个文件,然后将其发送到远程服务器。
它之前是使用 Jsch 实现的。然而,由于它依赖于 JCE 并且无法升级 java 版本(从 5 开始),我们正在切换到 Ganymed。我正在使用 Ganymed build 210(即针对 java 5 进行了测试;http://www.ganymed.ethz.ch/ssh2)
这是我用来 sftp 文件的函数。
public boolean sftp_put() {
File privateKeyFile = new File(identityPath);
File rfile = new File(hostDir);
File lfile = new File(lpath);
boolean success = false;
try {
if (!lfile.exists() || lfile.isDirectory()) {
throw new IOException("Local file must be a regular file: "
+ lpath);
}
Connection ssh = new Connection(host, port);
ssh.connect();
ssh.authenticateWithPublicKey(user, privateKeyFile, password);
SFTPv3Client sftp = new SFTPv3Client(ssh);
try {
SFTPv3FileAttributes attr = sftp.lstat(hostDir);
if (attr.isDirectory()) {
rfile = new File(hostDir, lfile.getName());
}
} catch (SFTPException e) {
try {
SFTPv3FileAttributes attr = sftp.lstat(rfile.getParent());
if (!attr.isDirectory()) {
throw new IOException(
"Remote file's parent must be a directory: "
+ hostDir + "," + e);
}
} catch (SFTPException ex) {
throw new IOException(
"Remote file's parent directory must exist: "
+ hostDir + "," + ex);
}
}
SFTPv3FileHandle file = sftp.createFileTruncate(rfile
.getCanonicalPath());
long fileOffset = 0;
byte[] src = new byte[32768];
int i = 0;
FileInputStream input = new FileInputStream(lfile);
while ((i = input.read(src)) != -1) {
sftp.write(file, fileOffset, src, 0, i);
fileOffset += i;
}
input.close();
sftp.closeFile(file);
sftp.close();
success=true;
} catch (IOException e1) {
logger.warn("Exception while trying to sftp", e)
}
return success;
}
我无法连接到远程服务器,可能是由于绑定(bind)问题,并且不确定如何继续?我正在考虑在 SFTP 之前绑定(bind)本地地址。
所以我写了一个socket函数。
public Socket createSocket(String destinationHost, int destinationPort)
throws IOException, UnknownHostException {
logger.info("sftp configured bind address : " + bindAddress
+ ", bind port : " + bindPort);
Socket socket = new Socket();
socket.bind(new InetSocketAddress(bindAddress, bindPort));
socket.connect(new InetSocketAddress(destinationHost, destinationPort),
connectionTimeOut);
if (socket.isBound()) {
logger.info("sftp actual bind port : " + socket.getLocalPort());
} else {
logger.warn("sftp socket not bound to local port");
}
return socket;
}
但是这也不起作用,并且我收到了套接字异常。
编辑:所以我以正确的方式创建了套接字,但我在哪里使用相同的套接字来创建连接。任何 Ganymed 库中均未定义此类方法。
最佳答案
由于ganymed中没有固有的方法,所以我编辑了源代码编写了一个方法。
以下是我所做的编辑。
到我正在使用的类
SocketAddress sourceAddress = new InetSocketAddress(bindAddress,
bindPort);
Connection ssh = new Connection(host, port);
ssh.bindSourceAddress(sourceAddress);
ssh.connect();
然后我对Ganymed API的connection.class做了一些更改。相应地导入类并声明变量
这是传递bindAddress 的简单方法。
public void bindSourceAddress(SocketAddress sourceAddress) {
this.sourceAddress = sourceAddress;
}
使用初始化方法时将地址传递给 Transport Manager 类。
if (sourceAddress != null) {
tm.initialize(cryptoWishList, verifier, dhgexpara,
connectTimeout, getOrCreateSecureRND(), proxyData,
sourceAddress);
} else {
tm.initialize(cryptoWishList, verifier, dhgexpara,
connectTimeout, getOrCreateSecureRND(), proxyData);
}
修改了initialize方法的构造函数。它依次调用建立连接函数,该函数经过类似修改以适应 SocketAddress。
private void establishConnection(ProxyData proxyData, int connectTimeout, SocketAddress sourceAddress) throws IOException
{
/* See the comment for createInetAddress() */
if (proxyData == null)
{
InetAddress addr = createInetAddress(hostname);
//test
if (sourceAddress != null) {
sock.bind(sourceAddress);
}
sock.connect(new InetSocketAddress(addr, port), connectTimeout);
sock.setSoTimeout(0);
return;
}
if (proxyData instanceof HTTPProxyData)
{
HTTPProxyData pd = (HTTPProxyData) proxyData;
/* At the moment, we only support HTTP proxies */
InetAddress addr = createInetAddress(pd.proxyHost);
//test
if (sourceAddress != null) {
sock.bind(sourceAddress);
}
sock.connect(new InetSocketAddress(addr, pd.proxyPort), connectTimeout);
sock.setSoTimeout(0);
终于绑定(bind)了套接字。作为一名java新手,我花了我自己的一段美好时光才把这件事做好。很可能没有人会阅读此内容或需要它,但发布此解决方案以防万一像我这样的人!
关于java - 使用Ganymed API进行SFTP时如何绑定(bind)本地地址作为源地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34758639/
我是一名优秀的程序员,十分优秀!