下载依赖
用这个:
1 2 3 4 5 6 7 8 9 10 11
| <dependency> <groupId>com.github.mwiede</groupId> <artifactId>jsch</artifactId> <version>0.2.25</version> </dependency>
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.18</version> </dependency>
|
需要注意,下面这个依赖在某些时候有问题,尽量不要使用(具体啥问题忘了,遇到再补充)
1 2 3 4 5
| <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency>
|
sftp 案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| private void sendBySftp(String remotePath, File localPath,FileConfigurationProperties fileConfigurationProperties) { try (Sftp sftp = JschUtil.createSftp(fileConfigurationProperties.getHost(), fileConfigurationProperties.getPort(), fileConfigurationProperties.getUsername(), fileConfigurationProperties.getPassword())) { sftp.mkDirs(remotePath); if (sftp.exist(remotePath)) { sftp.upload(remotePath, localPath); log.info("成功发送文件到 {}", remotePath + File.separator + localPath.getName()); if (localPath.delete()) { log.info("本地文件已删除 {}", localPath.getAbsolutePath()); } } else { log.error("目录创建失败: {}", remotePath); throw new RuntimeException("目录创建失败: " + remotePath); } } catch (Exception e) { log.error("{},发送文件到{}失败 {}", localPath, remotePath, ExceptionUtil.stacktraceToString(e)); } }
|
ftp 案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| private void sendByFtp(String remotePath, File localPath,FileConfigurationProperties fileConfigurationProperties) { Ftp ftp = new Ftp(fileConfigurationProperties.getHost(), fileConfigurationProperties.getPort(), fileConfigurationProperties.getUsername(), fileConfigurationProperties.getPassword(), StandardCharsets.UTF_8); try { ftp.uploadFileOrDirectory(remotePath, localPath); } catch (Exception e) { log.error("发送文件失败 {}", ExceptionUtil.stacktraceToString(e)); } finally { try { ftp.close(); } catch (IOException e) { log.error("ftp 关闭失败 {}", ExceptionUtil.stacktraceToString(e)); } } }
|