java 连接 sftp失败_java – 文件上传到SFTP失败(Apache VFS)

我有一个SFTP问题(

Windows

WinSSHD).我尝试用

Apache Commons VFS在一个文件夹中写一个文件.在本地SFTP上我没有上传的问题,但在第二个SFTP上我总是得到以下错误.

FTP看起来像这样:

我需要上传到文件夹“alis”.奇怪的是它没有User / Group和770权限.但是,使用FileZilla文件上传工作正常(使用相同的登录).

在文件夹“alis”上执行“manager.resolveFile()”(我尝试上传到此文件夹)并打印“.getType()”我获取信息“文件”而不是预期的“文件夹”.

有没有人知道为什么VFS会将文件夹识别为文件或为什么上传不起作用?

将文件上载到SFTP时的例外情况:

Exception in thread "main" java.lang.RuntimeException: org.apache.commons.vfs2.FileSystemException: Could not copy "file:///D:/Test/test.txt" to "sftp://user:***@host/.../alis/test.txt".

at test.Test.upload(Test.java:77)

at test.Test.main(Test.java:22)

Caused by: org.apache.commons.vfs2.FileSystemException: Could not copy "file:///D:/Test/test.txt" to "sftp://user:***@host/.../alis/test.txt".

at org.apache.commons.vfs2.provider.AbstractFileObject.copyFrom(AbstractFileObject.java:1062)

at test.Test.upload(Test.java:73)

... 1 more

Caused by: org.apache.commons.vfs2.FileSystemException: Could not create folder "sftp://user:***@host/.../alis" because it already exists and is a file.

at org.apache.commons.vfs2.provider.AbstractFileObject.createFolder(AbstractFileObject.java:968)

at org.apache.commons.vfs2.provider.AbstractFileObject.getOutputStream(AbstractFileObject.java:1424)

at org.apache.commons.vfs2.provider.DefaultFileContent.getOutputStream(DefaultFileContent.java:461)

at org.apache.commons.vfs2.provider.DefaultFileContent.getOutputStream(DefaultFileContent.java:441)

at org.apache.commons.vfs2.FileUtil.copyContent(FileUtil.java:111)

at org.apache.commons.vfs2.provider.AbstractFileObject.copyFrom(AbstractFileObject.java:1053)

... 2 more

源代码:(运行示例,您需要“jsch-0.1.50.jar”)

import java.io.File;

import org.apache.commons.vfs2.FileObject;

import org.apache.commons.vfs2.FileSystemException;

import org.apache.commons.vfs2.FileSystemOptions;

import org.apache.commons.vfs2.Selectors;

import org.apache.commons.vfs2.impl.StandardFileSystemManager;

import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;

/**

*

* @author thbe

*/

public class Test {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

upload("host", "user", "password", "D:/Test/test.txt", "/../alis/test.txt");

}

public static FileSystemOptions createDefaultOptions()

throws FileSystemException {

// Create SFTP options

FileSystemOptions opts = new FileSystemOptions();

// SSH Key checking

SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(

opts, "no");

// Root directory set to user home

SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);

// Timeout is count by Milliseconds

SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

SftpFileSystemConfigBuilder.getInstance().setPreferredAuthentications(opts, "publickey,keyboard-interactive,password");

return opts;

}

public static void upload(String hostName, String username,

String password, String localFilePath, String remoteFilePath) {

File f = new File(localFilePath);

if (!f.exists()) {

throw new RuntimeException("Error. Local file not found");

}

StandardFileSystemManager manager = new StandardFileSystemManager();

try {

manager.init();

// Create local file object

FileObject localFile = manager.resolveFile(f.getAbsolutePath());

System.out.println("open remote File");

FileObject remoteFile = manager.resolveFile(

createConnectionString(hostName, username, password,

remoteFilePath), createDefaultOptions());

System.out.println("exists:"+remoteFile.exists());

System.out.println("type:"+remoteFile.getType());

System.out.println("URL:"+remoteFile.getURL());

System.out.println("copy to remote File");

remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);

System.out.println("File upload success");

} catch (Exception e) {

throw new RuntimeException(e);

} finally {

manager.close();

}

}

public static String createConnectionString(String hostName,

String username, String password, String remoteFilePath) {

// result: "sftp://user:123456@domainname.com/resume.pdf

return "sftp://" + username + ":" + password + "@" + hostName + "/"

+ remoteFilePath;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值