java 校验文件头 判断虚假文件

来源:互联网 发布:机械先驱知乎 编辑:程序博客网 时间:2024/06/06 01:53

写博客只为技术的点滴积累


    /**
     * 图片文件与视频文件检查
     * @param imgname
     * @param InputStream
     * @param filename
     * @return
     */
    public String checkImage(InputStream is, String filename) {
        //创建图片文件类
        File file = new File(filename);
        //将文件流写入到新的文件对象中
        inputstreamtofile(is, file);
        //获取文件后缀
        String prefix = filename.substring(filename.lastIndexOf(".")+1);

        try{
            //如果是图片文件
            if(!"mp4".equals(prefix)){
                //检测图片格式
                ImageInputStream oImage = ImageIO.createImageInputStream(file);
                Iterator<ImageReader> iter = ImageIO.getImageReaders(oImage);
                if (!iter.hasNext()) {
                    oImage.close();
                    file.delete();
                    String string="上传文件格式不正确,请上传图片或视频文件!";
                    return string;
                }
            
                //将文件转换为图片
                ImageReader reader = iter.next();
                reader.setInput(oImage, true);
                ImageReadParam param = reader.getDefaultReadParam();
                BufferedImage bi = reader.read(0, param);
                //得到图像实际大小
                double width = bi.getWidth();
                double height = bi.getHeight();
                double ratio = Math.round(width/height);
                if(ratio<1.8||ratio>2.2){
                    oImage.close();
                    file.delete();
                    String string ="上传图片长宽比不正确,请上传长宽比1.8~2.2的图片文件!";
                    return string;
                }
                oImage.close();    
                file.delete();
                //如果是视频文件
            }else if ("mp4".equals(prefix)){
                //校验伪装MP4
                //获取文件头类型
                final String fileType = GetTypeByHead.getFileHeader(filename);
                //如果文件头类型不是MP4文件

              //  下面方法 bytesToHexString() 获取文件头转化为16进制数组 mp4文件头恰好是00000020

                if(!fileType.equals("00000020")){
                    file.delete();
                    String string="上传文件格式不正确,请上传图片或视频文件!";
                    return string;
                }    
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return "success";
    }



    /**
     * 文件流写入到文件
     * @param InputStream
     * @param file
     * @return
     */
    public void inputstreamtofile(InputStream ins,File file){
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[10240];
            while ((bytesRead = ins.read(buffer, 0, 10240)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getFileHeader(String filePath) {
        System.out.println("getFileHeader");
        FileInputStream is = null;
        String value = null;
        try {
            is = new FileInputStream(filePath);
            byte[] b = new byte[4];
            /*
             * int read() 从此输入流中读取一个数据字节。int read(byte[] b) 从此输入流中将最多 b.length
             * 个字节的数据读入一个 byte 数组中。 int read(byte[] b, int off, int len)
             * 从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。
             */
            is.read(b, 0, b.length);
            value = bytesToHexString(b);
            System.out.println(value);
        } catch (Exception e) {
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
        return value;
    }

    /**
     * 将要读取文件头信息的文件的byte数组转换成string类型表示
     *
     * @param src
     *            要读取文件头信息的文件的byte数组
     * @return 文件头信息
     */
    private static String bytesToHexString(byte[] src) {
        StringBuilder builder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        String hv;
        for (int i = 0; i < src.length; i++) {
            // 以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式,并转换为大写
            hv = Integer.toHexString(src[i] & 0xFF).toUpperCase();
            if (hv.length() < 2) {
                builder.append(0);
            }
            builder.append(hv);
        }
        return builder.toString();
    }

0 0