package cn.iocoder.yudao.module.infra.framework.file.core.utils; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.ttl.TransmittableThreadLocal; import jakarta.servlet.http.HttpServletResponse; import lombok.SneakyThrows; import org.apache.tika.Tika; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; /** * 文件类型 Utils * * @author 芋道源码 */ public class FileTypeUtils { private static final ThreadLocal TIKA = TransmittableThreadLocal.withInitial(Tika::new); /** * 获得文件的 mineType,对于doc,jar等文件会有误差 * * @param data 文件内容 * @return mineType 无法识别时会返回“application/octet-stream” */ @SneakyThrows public static String getMineType(byte[] data) { return TIKA.get().detect(data); } /** * 已知文件名,获取文件类型,在某些情况下比通过字节数组准确,例如使用jar文件时,通过名字更为准确 * * @param name 文件名 * @return mineType 无法识别时会返回“application/octet-stream” */ public static String getMineType(String name) { return TIKA.get().detect(name); } /** * 在拥有文件和数据的情况下,最好使用此方法,最为准确 * * @param data 文件内容 * @param name 文件名 * @return mineType 无法识别时会返回“application/octet-stream” */ public static String getMineType(byte[] data, String name) { return TIKA.get().detect(data, name); } /** * 返回附件 * * @param response 响应 * @param filename 文件名 * @param content 附件内容 */ public static void writeAttachment(HttpServletResponse response, String filename, byte[] content) throws IOException { // 设置 header 和 contentType response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); String contentType = getMineType(content, filename); response.setContentType(contentType); // 针对 video 的特殊处理,解决视频地址在移动端播放的兼容性问题 if (StrUtil.containsIgnoreCase(contentType, "video")) { long contentLength = content.length; response.setHeader("Content-Length", String.valueOf(contentLength)); response.setHeader("Content-Range", "bytes 0-" + (contentLength - 1) + "/" + contentLength); response.setHeader("Accept-Ranges", "bytes"); } // 输出附件 OutputStream outputStream = null; try { outputStream = response.getOutputStream(); IoUtil.write(outputStream, false, content); } catch (IOException e) { if ("Connection reset by peer".equals(e.getMessage())) { System.out.println("客户端中断连接: {}"); System.out.println(e.getMessage()); } else { throw e; } } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { System.out.println("关闭输出流失败: {}"); System.out.println(e.getMessage()); } } } } }