基于 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

@ -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));
}
}