基于 tika 识别文件类型

This commit is contained in:
YunaiV 2022-07-08 21:26:34 +08:00
parent 2372c25e8d
commit ed097e5ead
6 changed files with 49 additions and 5 deletions

View File

@ -58,6 +58,7 @@
<transmittable-thread-local.version>2.12.2</transmittable-thread-local.version>
<commons-net.version>3.8.0</commons-net.version>
<jsch.version>0.1.55</jsch.version>
<tika-core.version>2.4.1</tika-core.version>
<!-- 三方云服务相关 -->
<minio.version>8.2.2</minio.version>
<aliyun-java-sdk-core.version>4.5.25</aliyun-java-sdk-core.version>
@ -480,6 +481,12 @@
<version>${easyexcel.verion}</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId> <!-- 文件类型的识别 -->
<version>${tika-core.version}</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>

View File

@ -58,4 +58,8 @@ public class FileUtils {
return file;
}
public static void main(String[] args) {
}
}

View File

@ -61,6 +61,11 @@
<artifactId>jsch</artifactId> <!-- 解决 sftp 连接 -->
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId> <!-- 文件类型的识别 -->
</dependency>
<!-- 三方云服务相关 -->
<dependency>
<groupId>io.minio</groupId>

View File

@ -0,0 +1,29 @@
package cn.iocoder.yudao.framework.file.core.utils;
import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.SneakyThrows;
import org.apache.tika.Tika;
import java.io.ByteArrayInputStream;
/**
* 文件类型 Utils
*
* @author 芋道源码
*/
public class FileTypeUtils {
private static final ThreadLocal<Tika> TIKA = TransmittableThreadLocal.withInitial(Tika::new);
/**
* 获得文件的 mineType
*
* @param data 文件内容
* @return mineType
*/
@SneakyThrows
public static String getMineType(byte[] data) {
return TIKA.get().detect(new ByteArrayInputStream(data));
}
}

View File

@ -119,7 +119,6 @@
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-spring-boot-starter-file</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -1,11 +1,11 @@
package cn.iocoder.yudao.module.infra.service.file;
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.file.core.client.FileClient;
import cn.iocoder.yudao.framework.file.core.utils.FileTypeUtils;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper;
@ -13,7 +13,6 @@ import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS;
@ -41,9 +40,10 @@ public class FileServiceImpl implements FileService {
@SneakyThrows
public String createFile(String name, String path, byte[] content) {
// 计算默认的 path
String type = FileTypeUtil.getType(new ByteArrayInputStream(content), name);
String type = FileTypeUtils.getMineType(content);
if (StrUtil.isEmpty(path)) {
path = DigestUtil.md5Hex(content) + '.' + type;
path = DigestUtil.md5Hex(content)
+ '.' + StrUtil.subAfter(type, '/', true); // 文件的后缀
}
// 如果 name 为空则使用 path 填充
if (StrUtil.isEmpty(name)) {