判断图片文件真实类型---通过文件表头判断。

■前言

BufferedImage image = ImageIO.read(bin);

的返回值为NULL,所以想要查看图片文件真实的类型。

    /**
     * tif合并
     **/
    public static byte[] margerTif(List<byte[]> bytes){
        ByteArrayInputStream bin = null;
        List<BufferedImage> images = new ArrayList<BufferedImage> ();
        for(byte[] b : bytes) {
            try {
                bin = new ByteArrayInputStream(b);
                BufferedImage image = ImageIO.read(bin);
                images.add(image);
                bin.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

代码:TIF合并Java代码_com.sun.media.jai.codec-CSDN博客

■例子

tif

- 文件头标识 (2 bytes) 4D 4D 或 49 49

・画图软件,以TIF形式保存文件

Java代码,合并生成的TIF (代码:TIF合并Java代码_com.sun.media.jai.codec-CSDN博客

=====

Get-Content ./1.tif | Format-Hex -Raw | Select-Object -First 1

===

■标头

========

(1)JPEG

- 文件头标识 (2 bytes): 0xff, 0xd8 (SOI) (JPEG 文件标识)

- 文件结束标识 (2 bytes): 0xff, 0xd9 (EOI)

(2)TGA

- 未压缩的前5字节 00 00 02 00 00

- RLE压缩的前5字节 00 00 10 00 00

(3)PNG

- 文件头标识 (8 bytes) 89 50 4E 47 0D 0A 1A 0A     字符包含:P NG 

                                                                                   P(50) N(4E) G(47) 

(4)GIF

- 文件头标识 (6 bytes) 47 49 46 38 39(37) 61,字符即: G I F 8 9 (7) a

(5)BMP

- 文件头标识 (2 bytes) 42 4D,字符即: B M

注意:有四种格式的 BMP 

  (通常使用第一个,【单色位图】)

===

图片

===
四种形式文件大小

Get-Content .\type1.bmp | Format-Hex -Raw | Select-Object -First 1 | findstr "000000"


Get-Content .\type2.bmp | Format-Hex -Raw | Select-Object -First 2 | findstr "000000"


Get-Content .\type3.bmp | Format-Hex -Raw | Select-Object -First 2 | findstr "000000"


Get-Content .\type4.bmp | Format-Hex -Raw | Select-Object -First 2 | findstr "000000"

四种形式文件16进制显示

===

(6)TIFF

- 文件头标识 (2 bytes) 4D 4D 或 49 49

(7)ICO

- 文件头标识 (8 bytes) 00 00 01 00 01 00 20 20

(8)CUR

- 文件头标识 (8 bytes) 00 00 02 00 01 00 20 20

■判断方法,16进制形式查看文件

win10 以 十六进制 形式(方式) 查看文件 内容_怎么用win10自带的format-hex工具-CSDN博客

・TIF

---

・PNG

package com.ylw.p2p.common.utils; import java.awt.image.BufferedImage; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FileUtils { public final static Map IMG_FILE_TYPE_MAP = new HashMap(); /** * @Description: 图片文件上传 * @author Xiao.Sky * @creaetime 2015年4月17日下午5:20:27 * @param request * @param response * @param photo * @param strtmp * 文件名称 xxx.jpg * @param path * 文件路径 * @param num * @return */ public static boolean updatePhoto(HttpServletRequest request,HttpServletResponse response, File photo, String strtmp,String path, long num) { File dir = new File(path); // 如果不存在就创建次文件夹 if (!dir.exists()) { dir.mkdirs(); } File newFile = new File(dir, strtmp); // 如果存在此文件就删除此文件 if (newFile.exists()) newFile.delete(); BufferedInputStream bis = null; FileInputStream fis = null; try { fis = new FileInputStream(photo); FileOutputStream fos = new FileOutputStream(newFile); BufferedImage src = ImageIO.read(fis); ImageIO.write(src, "png", fos); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != bis) { bis.close(); } if (null != fis) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } return true; } /** * * @Description: 普通文件上传 * @author Xiao.Sky * @creaetime 2015年4月23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值