用java流方式判斷檔案類型

來源:互聯網
上載者:User

標籤:

這個方法只能在有限的範圍內有效。並不是萬金油

比如

圖片類型判斷,音頻檔案格式判斷,視頻檔案格式判斷等這種肯定是2進位且專業性很強的檔案類型判斷。

下面給出完整版代碼

首先是檔案類型枚取

package org.filetype;/** * 檔案類型枚取 */public enum FileType {/** * JEPG. */JPEG("FFD8FF"),/** * PNG. */PNG("89504E47"),/** * GIF. */GIF("47494638"),/** * TIFF. */TIFF("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). */PST("2142444E"),/** * MS Word/Excel. */XLS_DOC("D0CF11E0"),/** * MS Access. */MDB("5374616E64617264204A"),/** * WordPerfect. */WPD("FF575043"),/** * Postscript. */EPS("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). */MPG("000001BA"),/** * Quicktime. */MOV("6D6F6F76"),/** * Windows Media. */ASF("3026B2758E66CF11"),/** * MIDI. */MID("4D546864");private String value = "";/** * Constructor. *  * @param type  */private FileType(String value) {this.value = value;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}}

  然後是類型判斷核心類

package org.filetype;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;/** * 檔案類型判斷類 */public final class FileTypeJudge {/** * Constructor */private FileTypeJudge() {}/** * 將檔案頭轉換成16進位字串 *  * @param 原生byte * @return 16進位字串 */private 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 filePath 檔案路徑 * @return 檔案頭 * @throws IOException */private static String getFileContent(String filePath) throws IOException {byte[] b = new byte[28];InputStream inputStream = null;try {inputStream = new FileInputStream(filePath);inputStream.read(b, 0, 28);} catch (IOException e) {e.printStackTrace();throw e;} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {e.printStackTrace();throw e;}}}return bytesToHexString(b);}/** * 判斷檔案類型 *  * @param filePath 檔案路徑 * @return 檔案類型 */public static FileType getType(String filePath) throws IOException {String fileHead = getFileContent(filePath);if (fileHead == null || fileHead.length() == 0) {return null;}fileHead = fileHead.toUpperCase();FileType[] fileTypes = FileType.values();for (FileType type : fileTypes) {if (fileHead.startsWith(type.getValue())) {return type;}}return null;}}

  最後是測試類別

package org.filetype;public class Test {/** * @param args */public static void main(String args[]) throws Exception {System.out.println(FileTypeJudge.getType("C:\\eclipse-jee-helios-win32.zip"));}}

  

用java流方式判斷檔案類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.