Commit d8a242629b4d4b3ccc6ec0e492552021c8e2dcd8
Merge branch 'feature/1.6.2' of https://gitee.com/zhijiantianya/ruoyi-vue-pro into feature/1.6.2
Showing
1 changed file
with
10 additions
and
5 deletions
yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/ftp/FtpFileClient.java
| @@ -27,9 +27,11 @@ public class FtpFileClient extends AbstractFileClient<FtpFileClientConfig> { | @@ -27,9 +27,11 @@ public class FtpFileClient extends AbstractFileClient<FtpFileClientConfig> { | ||
| 27 | 27 | ||
| 28 | @Override | 28 | @Override |
| 29 | protected void doInit() { | 29 | protected void doInit() { |
| 30 | - // 补全风格。例如说 Linux 是 /,Windows 是 \ | ||
| 31 | - if (!config.getBasePath().endsWith(File.separator)) { | ||
| 32 | - config.setBasePath(config.getBasePath() + File.separator); | 30 | + // 把配置的 \ 替换成 /, 如果路径配置 \a\test, 替换成 /a/test, 替换方法已经处理 null 情况 |
| 31 | + config.setBasePath(StrUtil.replace(config.getBasePath(), StrUtil.BACKSLASH, StrUtil.SLASH)); | ||
| 32 | + // ftp的路径是 / 结尾 | ||
| 33 | + if (!config.getBasePath().endsWith(StrUtil.SLASH)) { | ||
| 34 | + config.setBasePath(config.getBasePath() + StrUtil.SLASH); | ||
| 33 | } | 35 | } |
| 34 | // 初始化 Ftp 对象 | 36 | // 初始化 Ftp 对象 |
| 35 | this.ftp = new Ftp(config.getHost(), config.getPort(), config.getUsername(), config.getPassword(), | 37 | this.ftp = new Ftp(config.getHost(), config.getPort(), config.getUsername(), config.getPassword(), |
| @@ -42,6 +44,7 @@ public class FtpFileClient extends AbstractFileClient<FtpFileClientConfig> { | @@ -42,6 +44,7 @@ public class FtpFileClient extends AbstractFileClient<FtpFileClientConfig> { | ||
| 42 | String filePath = getFilePath(path); | 44 | String filePath = getFilePath(path); |
| 43 | String fileName = FileUtil.getName(filePath); | 45 | String fileName = FileUtil.getName(filePath); |
| 44 | String dir = StrUtil.removeSuffix(filePath, fileName); | 46 | String dir = StrUtil.removeSuffix(filePath, fileName); |
| 47 | + ftp.reconnectIfTimeout(); | ||
| 45 | boolean success = ftp.upload(dir, fileName, new ByteArrayInputStream(content)); | 48 | boolean success = ftp.upload(dir, fileName, new ByteArrayInputStream(content)); |
| 46 | if (!success) { | 49 | if (!success) { |
| 47 | throw new FtpException(StrUtil.format("上传文件到目标目录 ({}) 失败", filePath)); | 50 | throw new FtpException(StrUtil.format("上传文件到目标目录 ({}) 失败", filePath)); |
| @@ -53,6 +56,7 @@ public class FtpFileClient extends AbstractFileClient<FtpFileClientConfig> { | @@ -53,6 +56,7 @@ public class FtpFileClient extends AbstractFileClient<FtpFileClientConfig> { | ||
| 53 | @Override | 56 | @Override |
| 54 | public void delete(String path) { | 57 | public void delete(String path) { |
| 55 | String filePath = getFilePath(path); | 58 | String filePath = getFilePath(path); |
| 59 | + ftp.reconnectIfTimeout(); | ||
| 56 | ftp.delFile(filePath); | 60 | ftp.delFile(filePath); |
| 57 | } | 61 | } |
| 58 | 62 | ||
| @@ -60,8 +64,9 @@ public class FtpFileClient extends AbstractFileClient<FtpFileClientConfig> { | @@ -60,8 +64,9 @@ public class FtpFileClient extends AbstractFileClient<FtpFileClientConfig> { | ||
| 60 | public byte[] getContent(String path) { | 64 | public byte[] getContent(String path) { |
| 61 | String filePath = getFilePath(path); | 65 | String filePath = getFilePath(path); |
| 62 | String fileName = FileUtil.getName(filePath); | 66 | String fileName = FileUtil.getName(filePath); |
| 63 | - String dir = StrUtil.removeSuffix(path, fileName); | 67 | + String dir = StrUtil.removeSuffix(filePath, fileName); |
| 64 | ByteArrayOutputStream out = new ByteArrayOutputStream(); | 68 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 69 | + ftp.reconnectIfTimeout(); | ||
| 65 | ftp.download(dir, fileName, out); | 70 | ftp.download(dir, fileName, out); |
| 66 | return out.toByteArray(); | 71 | return out.toByteArray(); |
| 67 | } | 72 | } |
| @@ -70,4 +75,4 @@ public class FtpFileClient extends AbstractFileClient<FtpFileClientConfig> { | @@ -70,4 +75,4 @@ public class FtpFileClient extends AbstractFileClient<FtpFileClientConfig> { | ||
| 70 | return config.getBasePath() + path; | 75 | return config.getBasePath() + path; |
| 71 | } | 76 | } |
| 72 | 77 | ||
| 73 | -} | 78 | -} |
| 79 | +} | ||
| 74 | \ No newline at end of file | 80 | \ No newline at end of file |