1. 程式人生 > >[筆記]jsp 前臺展示多個PDF和JPG

[筆記]jsp 前臺展示多個PDF和JPG

之前是從前臺接收了一個檔案ID引數

這個功能的開發人員將各類檔案ID放在了一起,使用^隔開

檔案包括jpg、png、pdf

思路:

後臺接收-查詢對應檔案流-判斷檔案型別-前臺展示

程式碼:

判斷檔案型別的方法摒棄了傳統的判斷檔名字尾的方法,因為檔名存在被更改的情況

於是採用了網上所說的通過檔案流頭的方法

各類常見檔案頭:

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 final String TYPE_JPG = "jpg";
    public static final String TYPE_GIF = "gif";
    public static final String TYPE_PNG = "png";
    public static final String TYPE_PDF = "pdf";
    public static final String TYPE_UNKNOWN = "unknown";
    /**
     * byte陣列轉換成16進位制字串
     * @param src
     * @return
     */
    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();
    }

    /**
     * 根據檔案流判斷圖片型別
     * @return jpg/png/gif/bmp
     */
    public static String getPicType(byte[] bytes) {
        //讀取檔案的前幾個位元組來判斷圖片格式
        byte[] b =Arrays.copyOfRange(bytes,0,4);
        try {
            String type = bytesToHexString(b).toUpperCase();
            if (type.contains("FFD8FF")) {
                return TYPE_JPG;
            } else if (type.contains("89504E47")) {
                return TYPE_PNG;
            } else if (type.contains("47494638")) {
                return TYPE_GIF;
            } else if (type.contains("255044462D312E")) {
                return TYPE_PDF;
            }else{
                return TYPE_UNKNOWN;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根據檔案流判斷圖片型別
     * @return jpg/png/gif/bmp
     */
    public static String getPicType(byte[] bytes) {
        //讀取檔案的前幾個位元組來判斷圖片格式
        byte[] b =Arrays.copyOfRange(bytes,0,4);
        try {
            String type = bytesToHexString(b).toUpperCase();
            if (type.contains("FFD8FF")) {
                return TYPE_JPG;
            } else if (type.contains("89504E47")) {
                return TYPE_PNG;
            } else if (type.contains("47494638")) {
                return TYPE_GIF;
            } else if (type.contains("255044462D312E")) {
                return TYPE_PDF;
            }else{
                return TYPE_UNKNOWN;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

前臺獲取: 

OVER