【sftp】java使用vfs2传输sftp失败,提示Could not find file with URI because it is a relative path

如果自己的链接能通过工具访问,可能是缺少依赖:
JSch、commons-logging 和 commons-Lang。
在这里插入图片描述
添加依赖后,虽然提示permission有问题,但是正常上传了。
参考https://stackoverflow.com/questions/68987411/apache-vfs-error-because-it-is-a-relative-path-and-no-base-uri-was-provided

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java实现SFTP文件传输,可以使用JSch库。以下是一个简单的示例代码,演示如何使用JSch实现SFTP上传文件到资源服务器: ```java import com.jcraft.jsch.*; public class SFTPUploader { public static void main(String[] args) { String host = "sftp.example.com"; String username = "your-username"; String password = "your-password"; int port = 22; String localFilePath = "/path/to/local/file.txt"; String remoteFilePath = "/path/to/remote/file.txt"; JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession(username, host, port); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword(password); session.connect(); ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); channelSftp.put(localFilePath, remoteFilePath); channelSftp.disconnect(); } catch (JSchException | SftpException e) { e.printStackTrace(); } finally { if (session != null) { session.disconnect(); } } } } ``` 在这个示例代码中,`host`是资源服务器的主机名,`username`和`password`是SFTP登录凭据,`port`是SFTP端口,默认为22。`localFilePath`是本地文件路径,`remoteFilePath`是远程文件路径。 `JSch`实例用于创建SFTP会话,`Session`实例用于连接到资源服务器。在此示例代码中,我们使用`StrictHostKeyChecking`配置来禁用主机密钥检查。接下来,我们打开一个SFTP通道,并将本地文件上传到远程服务器。 请注意,如果您需要进行多个文件传输,则应该重用单个`Session`和`ChannelSftp`实例,而不是每次传输都打开和关闭新的会话和通道。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值