fix:【infra 基础设施】LocalFileClient 读取不到文件(getContent)时,返回 null 而不是抛出异常 https://github.com/YunaiV/ruoyi-vue-pro/issues/929
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package cn.iocoder.yudao.module.infra.framework.file.core.client.local;
|
package cn.iocoder.yudao.module.infra.framework.file.core.client.local;
|
||||||
|
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.io.IORuntimeException;
|
||||||
import cn.iocoder.yudao.module.infra.framework.file.core.client.AbstractFileClient;
|
import cn.iocoder.yudao.module.infra.framework.file.core.client.AbstractFileClient;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -38,7 +39,14 @@ public class LocalFileClient extends AbstractFileClient<LocalFileClientConfig> {
|
|||||||
@Override
|
@Override
|
||||||
public byte[] getContent(String path) {
|
public byte[] getContent(String path) {
|
||||||
String filePath = getFilePath(path);
|
String filePath = getFilePath(path);
|
||||||
return FileUtil.readBytes(filePath);
|
try {
|
||||||
|
return FileUtil.readBytes(filePath);
|
||||||
|
} catch (IORuntimeException ex) {
|
||||||
|
if (ex.getMessage().startsWith("File not exist:")) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getFilePath(String path) {
|
private String getFilePath(String path) {
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import cn.iocoder.yudao.module.infra.framework.file.core.client.local.LocalFileC
|
|||||||
import org.junit.jupiter.api.Disabled;
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
|
||||||
|
|
||||||
public class LocalFileClientTest {
|
public class LocalFileClientTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -26,4 +28,18 @@ public class LocalFileClientTest {
|
|||||||
client.delete(path);
|
client.delete(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
public void testGetContent_notFound() {
|
||||||
|
// 创建客户端
|
||||||
|
LocalFileClientConfig config = new LocalFileClientConfig();
|
||||||
|
config.setDomain("http://127.0.0.1:48080");
|
||||||
|
config.setBasePath("/Users/yunai/file_test");
|
||||||
|
LocalFileClient client = new LocalFileClient(0L, config);
|
||||||
|
client.init();
|
||||||
|
// 上传文件
|
||||||
|
byte[] content = client.getContent(randomString());
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user