sftpChannel.put 上传文件覆盖
时间: 2023-11-24 16:05:13 浏览: 487
如果你想使用SFTP的put方法上传文件,并且想覆盖已存在的文件,可以设置put方法的第二个参数为ChannelSftp.OVERWRITE。例如:
sftpChannel.put(localFilePath, remoteFilePath, ChannelSftp.OVERWRITE);
其中,localFilePath为本地文件路径,remoteFilePath为远程文件路径,ChannelSftp.OVERWRITE表示如果远程文件已经存在,则覆盖它。如果不设置第二个参数或者设置为默认值null,则put方法默认不会覆盖已存在的文件。
相关问题
libssh2里sftp 上传文件
在libssh2库中,SFTP(Secure File Transfer Protocol)是一种安全的文件传输协议,用于通过SSH(Secure Shell)连接上传和下载文件。下面是使用libssh2在C语言环境中进行SFTP上传文件的基本步骤:
- 初始化libssh2:
首先需要包含
libssh2.h
头文件,并创建一个libssh2_session
结构实例。
#include <libssh2.h>
libssh2_session *session;
- 建立SSH会话:
使用
libssh2_init()
初始化库,然后使用libssh2_session_init()
创建一个新的会话。
if (libssh2_init() != LIBSSH2_SUCCESS) {
// 错误处理...
}
session = libssh2_session_new();
if (!session) {
// 错误处理...
}
- 连接到服务器:
设置主机名、端口号,以及用户名和密码,然后使用
libssh2_auth_password()
进行身份验证。
if (libssh2_hostkey_check(session, host_key_data, host_key_len) == LIBSSH2_ERROR_HOSTKEY_UNVERIFIABLE) {
// 没有信任的主机密钥,可以添加到~/.ssh/known_hosts中...
}
if (libssh2_connect(session, hostname, port) != LIBSSH2_OK) {
// 连接失败...
}
- 打开SFTP通道:
调用
libssh2_sftp_init()
函数打开SFTP通道。
libssh2_sftp* sftp = NULL;
if ((sftp = libssh2_sftp_init(session)) == NULL) {
// SFTP 初始化失败...
}
- 上传文件:
使用
libssh2_sftp_open()
打开远程目录,然后使用libssh2_sftp_put()
将本地文件内容写入远程路径。
// 打开远程目录
char* remote_dir_path = "path/to/remote/directory";
sftp_channel *sftp_fd;
if (libssh2_sftp_opendir(sftp, remote_dir_path, &sftp_fd) != LIBSSH2_OK) {
// 目录不存在,可能需要创建...
}
// 找到远程文件的完整路径
char* remote_file_path = malloc(strlen(remote_dir_path) + strlen(file_name) + 1);
strcpy(remote_file_path, remote_dir_path);
strcat(remote_file_path, "/");
strcat(remote_file_path, file_name);
// 上传文件
int local_fd, remote_offset = 0;
if (libssh2_sftp_fopen_ex(sftp_fd, remote_file_path, "wb", &local_fd) != LIBSSH2_OK) {
// 文件已存在,可以考虑覆盖...
}
size_t bytes_written = 0;
while ((bytes_written = read(local_fd, data, BUF_SIZE)) > 0) {
if (libssh2_sftp_write(sftp, local_fd, data, bytes_written, &remote_offset) != LIBSSH2_OK) {
break; // 处理错误...
}
}
// 关闭资源
fclose(local_fd);
libssh2_sftp_fclose(sftp_fd);
libssh2_sftp_close(sftp);
- 关闭并结束会话: 最后,记得关闭SFTP通道、清理内存和断开SSH连接。
libssh2_sftp_shutdown(sftp);
libssh2_sftp_free(&sftp);
libssh2_channel_free(&sftp_fd);
libssh2_exit(session, 0);
free(remote_file_path);
sftp上传gbk文件夹
使用 SFTP 上传包含 GBK 编码文件的文件夹
为了确保通过 SFTP 正确处理并上传包含 GBK 编码文件名的文件夹,可以采取以下方法:
配置 JSCH 库中的字符集编码
JSCH 默认可能不支持特定的字符集配置。因此,在初始化 ChannelSftp
对象之前,应该设置 JVM 的默认字符集为 GBK 或者在连接时指定字符集。
System.setProperty("sun.jnu.encoding", "GBK");
System.setProperty("file.encoding", "GBK");
创建自定义 ChannelSftp 类来覆盖原有的 setFilenameEncoding 方法
由于直接调用 setFilenameEncoding()
可能不起作用[^3],可以通过继承 ChannelSftp
并重写其内部逻辑以强制使用所需的编码方式。
public class CustomChannelSftp extends ChannelSftp {
@Override
protected String getRemoteFile(String path) throws Exception {
byte[] bytes = path.getBytes(Charset.forName("GBK"));
return new String(bytes, Charset.defaultCharset());
}
}
实现完整的 SFTP 文件传输功能
下面是一个简单的例子展示如何利用上述修改后的类来进行整个目录下所有文件及其子目录内文件的上传操作。
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
// ...其他必要的导入语句...
private static void uploadDirectory(CustomChannelSftp channelSftp, File localDir, String remoteBasePath) throws Exception {
if (!localDir.isDirectory()) throw new IllegalArgumentException("Source must be a directory");
for (File file : Objects.requireNonNull(localDir.listFiles())) {
final var relativePath = localDir.toURI().relativize(file.toURI()).getPath();
final var remoteFilePath = Paths.get(remoteBasePath, relativePath).toString();
if (file.isFile()) {
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
channelSftp.put(inputStream, remoteFilePath);
}
} else if (file.isDirectory()){
channelSftp.mkdir(remoteFilePath);
uploadDirectory(channelSftp, file, remoteFilePath);
}
}
}
public static void main(String[] args){
Session session=null;
Channel channel=null;
try{
JSch jsch=new JSch();
session=jsch.getSession(username, host, port);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel=session.openChannel("sftp");
channel.connect();
CustomChannelSftp customChannel=(CustomChannelSftp)channel;
// 调用函数执行实际的任务
uploadDirectory(customChannel, new File("/path/to/local/folder"), "/remote/path/");
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
if(session!=null && !session.isConnected())
session.disconnect();
if(channel!=null && channel.isConnected())
channel.disconnect();
}
}
此代码片段展示了如何创建一个名为 uploadDirectory
的辅助方法用于递归地遍历本地文件系统并将每个条目复制到远程位置;同时也包含了建立 SSH 连接以及断开连接的过程。
相关推荐













