您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關實現判斷java文件類型TikaToolKit,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
引入依賴
<dependency> <groupId>org.apache.tika</groupId> <artifactId>tika-core</artifactId> <version>1.20</version> </dependency>
或
compile 'org.apache.tika:tika-core:1.20'
工具類
import lombok.AllArgsConstructor; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.apache.tika.metadata.HttpHeaders; import org.apache.tika.metadata.Metadata; import org.apache.tika.metadata.TikaMetadataKeys; import org.apache.tika.parser.AutoDetectParser; import org.apache.tika.parser.ParseContext; import org.xml.sax.helpers.DefaultHandler; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.HashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * tika * * @author zhy */ @Slf4j public class TikaToolUtil { /** * 獲取類型 * * @param file 文件 * @return String */ public static String getMimeType(File file) { if (file.isDirectory()) { return "the target is a directory"; } AutoDetectParser parser = new AutoDetectParser(); parser.setParsers(new HashMap<>()); Metadata metadata = new Metadata(); metadata.add(TikaMetadataKeys.RESOURCE_NAME_KEY, file.getName()); try (InputStream stream = new FileInputStream(file)) { parser.parse(stream, new DefaultHandler(), metadata, new ParseContext()); } catch (Exception e) { log.error(e.getMessage()); } return metadata.get(HttpHeaders.CONTENT_TYPE); } /** * 判斷是否是圖片 * * @param file 文件 * @return boolean */ public static boolean isImage(File file) { String type = getMimeType(file); System.out.println(type); Pattern p = Pattern.compile(Type.IMAGE.name); Matcher m = p.matcher(type); return m.matches(); } /** * 判斷是否是zip * * @param file 文件 * @return boolean */ public static boolean isZip(File file) { String type = getMimeType(file); System.out.println(type); Pattern p = Pattern.compile(Type.ZIP.name); Matcher m = p.matcher(type); return m.matches(); } /** * 判斷是否是dicom * * @param file 文件 * @return boolean */ public static boolean isDicom(File file) { String type = getMimeType(file); Pattern p = Pattern.compile(Type.DCM.name); Matcher m = p.matcher(type); return m.matches(); } /** * 類型枚舉 */ @Getter @AllArgsConstructor public enum Type { /** * dcm */ DCM("application/dicom"), /** * zip */ ZIP("application/zip"), /** * 圖片 */ IMAGE("image/.*"), /** * 視頻 */ VIDEO("video/*"); private String name; } public static void main(String[] args) { boolean image = isImage(new File("C:\\Users\\zhy\\Desktop\\dcm\\123.dcm")); System.out.println("zhy:" + image); } }
補充知識:java根據文件流判斷文件類型(后綴名)
我就廢話不多說了,大家還是直接看代碼吧~
import java.io.FileInputStream; public class FileType{ public static String bytesToHexString(byte[] src) { StringBuilder stringBuilder = new StringBuilder(); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } /** * @param args */ public static void main(String[] args) throws Exception { FileInputStream is = new FileInputStream("F:\\相冊\\微信圖片\\03.jpg"); byte[] b = new byte[3]; is.read(b, 0, b.length); String xxx = bytesToHexString(b); xxx = xxx.toUpperCase(); System.out.println("頭文件是:" + xxx); String ooo = TypeDict.checkType(xxx); System.out.println("后綴名是:" + ooo); } }
public class TypeDict { /** 常用文件的文件頭如下:(以前六位為準) JPEG (jpg),文件頭:FFD8FF PNG (png),文件頭:89504E47 GIF (gif),文件頭:47494638 TIFF (tif),文件頭:49492A00 Windows Bitmap (bmp),文件頭:424D CAD (dwg),文件頭:41433130 Adobe Photoshop (psd),文件頭:38425053 Rich Text Format (rtf),文件頭:7B5C727466 XML (xml),文件頭:3C3F786D6C HTML (html),文件頭:68746D6C3E Email [thorough only] (eml),文件頭:44656C69766572792D646174653A Outlook Express (dbx),文件頭:CFAD12FEC5FD746F Outlook (pst),文件頭:2142444E MS Word/Excel (xls.or.doc),文件頭:D0CF11E0 MS Access (mdb),文件頭:5374616E64617264204A WordPerfect (wpd),文件頭:FF575043 Postscript (eps.or.ps),文件頭:252150532D41646F6265 Adobe Acrobat (pdf),文件頭:255044462D312E Quicken (qdf),文件頭:AC9EBD8F Windows Password (pwl),文件頭:E3828596 ZIP Archive (zip),文件頭:504B0304 RAR Archive (rar),文件頭:52617221 Wave (wav),文件頭:57415645 AVI (avi),文件頭:41564920 Real Audio (ram),文件頭:2E7261FD Real Media (rm),文件頭:2E524D46 MPEG (mpg),文件頭:000001BA MPEG (mpg),文件頭:000001B3 Quicktime (mov),文件頭:6D6F6F76 Windows Media (asf),文件頭:3026B2758E66CF11 MIDI (mid),文件頭:4D546864 */ public static String checkType(String xxxx) { switch (xxxx) { case "FFD8FF": return "jpg"; case "89504E": return "png"; case "474946": return "jif"; default: return "0000"; } } }
上述就是小編為大家分享的實現判斷java文件類型TikaToolKit了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。