Commit 4580bcf8e058c18beed5539e2c6642f74dc385ec

Authored by YunaiV
1 parent 3e3bcc96

fix:【infra 基础设施】LocalFileClient 读取不到文件(getContent)时,返回 null 而不是抛出异常 https://gith…

…ub.com/YunaiV/ruoyi-vue-pro/issues/929
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/local/LocalFileClient.java
1 1 package cn.iocoder.yudao.module.infra.framework.file.core.client.local;
2 2  
3 3 import cn.hutool.core.io.FileUtil;
  4 +import cn.hutool.core.io.IORuntimeException;
4 5 import cn.iocoder.yudao.module.infra.framework.file.core.client.AbstractFileClient;
5 6  
6 7 import java.io.File;
... ... @@ -38,7 +39,14 @@ public class LocalFileClient extends AbstractFileClient<LocalFileClientConfig> {
38 39 @Override
39 40 public byte[] getContent(String path) {
40 41 String filePath = getFilePath(path);
41   - return FileUtil.readBytes(filePath);
  42 + try {
  43 + return FileUtil.readBytes(filePath);
  44 + } catch (IORuntimeException ex) {
  45 + if (ex.getMessage().startsWith("File not exist:")) {
  46 + return null;
  47 + }
  48 + throw ex;
  49 + }
42 50 }
43 51  
44 52 private String getFilePath(String path) {
... ...
yudao-module-infra/src/test/java/cn/iocoder/yudao/module/infra/framework/file/core/local/LocalFileClientTest.java
... ... @@ -7,6 +7,8 @@ import cn.iocoder.yudao.module.infra.framework.file.core.client.local.LocalFileC
7 7 import org.junit.jupiter.api.Disabled;
8 8 import org.junit.jupiter.api.Test;
9 9  
  10 +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
  11 +
10 12 public class LocalFileClientTest {
11 13  
12 14 @Test
... ... @@ -26,4 +28,18 @@ public class LocalFileClientTest {
26 28 client.delete(path);
27 29 }
28 30  
  31 + @Test
  32 + @Disabled
  33 + public void testGetContent_notFound() {
  34 + // 创建客户端
  35 + LocalFileClientConfig config = new LocalFileClientConfig();
  36 + config.setDomain("http://127.0.0.1:48080");
  37 + config.setBasePath("/Users/yunai/file_test");
  38 + LocalFileClient client = new LocalFileClient(0L, config);
  39 + client.init();
  40 + // 上传文件
  41 + byte[] content = client.getContent(randomString());
  42 + System.out.println();
  43 + }
  44 +
29 45 }
... ...