wujian
2024-02-22 268beb4ebc1e5b8d4ad715b71cd64a0944073a87
提交 | 用户 | 时间
268beb 1 package com.jcdm.common.utils.file;
W 2
3 import java.io.File;
4 import org.apache.commons.lang3.StringUtils;
5
6 /**
7  * 文件类型工具类
8  *
9  * @author jc
10  */
11 public class FileTypeUtils
12 {
13     /**
14      * 获取文件类型
15      * <p>
16      * 例如: jcdm.txt, 返回: txt
17      * 
18      * @param file 文件名
19      * @return 后缀(不含".")
20      */
21     public static String getFileType(File file)
22     {
23         if (null == file)
24         {
25             return StringUtils.EMPTY;
26         }
27         return getFileType(file.getName());
28     }
29
30     /**
31      * 获取文件类型
32      * <p>
33      * 例如: jcdm.txt, 返回: txt
34      *
35      * @param fileName 文件名
36      * @return 后缀(不含".")
37      */
38     public static String getFileType(String fileName)
39     {
40         int separatorIndex = fileName.lastIndexOf(".");
41         if (separatorIndex < 0)
42         {
43             return "";
44         }
45         return fileName.substring(separatorIndex + 1).toLowerCase();
46     }
47
48     /**
49      * 获取文件类型
50      * 
51      * @param photoByte 文件字节码
52      * @return 后缀(不含".")
53      */
54     public static String getFileExtendName(byte[] photoByte)
55     {
56         String strFileExtendName = "JPG";
57         if ((photoByte[0] == 71) && (photoByte[1] == 73) && (photoByte[2] == 70) && (photoByte[3] == 56)
58                 && ((photoByte[4] == 55) || (photoByte[4] == 57)) && (photoByte[5] == 97))
59         {
60             strFileExtendName = "GIF";
61         }
62         else if ((photoByte[6] == 74) && (photoByte[7] == 70) && (photoByte[8] == 73) && (photoByte[9] == 70))
63         {
64             strFileExtendName = "JPG";
65         }
66         else if ((photoByte[0] == 66) && (photoByte[1] == 77))
67         {
68             strFileExtendName = "BMP";
69         }
70         else if ((photoByte[1] == 80) && (photoByte[2] == 78) && (photoByte[3] == 71))
71         {
72             strFileExtendName = "PNG";
73         }
74         return strFileExtendName;
75     }
76 }