sftp批量上传文件
时间: 2023-09-22 17:11:12 浏览: 258
要使用SFTP批量上传文件,你可以参考以下代码实例:
```
fromPath:本地文件夹的路径
upToPath:服务器目标文件夹的路径
public int uploadFiles(String fromPath, String upToPath) {
// 多文件上传
fromPath = judgePath(fromPath);
upToPath = judgePath(upToPath);
FileInputStream in = null;
String fileName = null;
String originPath = null;
File folder = new File(fromPath);
File[] listOfFiles = folder.listFiles();
try {
if (listOfFiles == null) {
return 0;
}
for (File file :listOfFiles) {
fileName = file.getName();
if (fileName.equals(".") || fileName.equals("..")) {
continue;
}
originPath = fromPath + fileName;
in = new FileInputStream(new File(originPath));
channelSftp.put(in, upToPath + fileName);
}
} catch (SftpException e) {
e.printStackTrace();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return listOfFiles.length;
}
```
这段代码实现了从本地文件夹`fromPath`批量上传文件到服务器目标文件夹`upToPath`。它首先遍历本地文件夹中的所有文件,然后将每个文件逐个上传到服务器。在上传过程中,使用`channelSftp.put(in, upToPath + fileName)`来将文件上传到指定目录。上传完成后,返回上传的文件数量。
请根据你的具体需求,修改代码中的路径和配置信息,以适应你的环境。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [JAVA SFTP文件上传、下载及批量下载实例](https://download.csdn.net/download/weixin_38577922/12784124)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [【Shell】linux使用SFTP批量上传](https://blog.csdn.net/luck_jinwei/article/details/130700615)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [java sftp 多文件上传下载](https://blog.csdn.net/Sharon_Yaoa/article/details/129995710)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文