java 获取文件的真实类型(不是根据文件的后缀名称判断类型)

java 获取文件的真实类型(不是根据文件的后缀名称判断类型)

依赖:java 获取文件的真实类型所需jar包


import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class OfficeFileTypeUtils {

    public static void main(String[] args) {
    	Scanner scan = new Scanner(System.in);
    	System.out.print("请输入文件路径:");
    	String filePath = scan.next();
    	System.out.println("=============================================\n");
        String realFilePath = getFileType(filePath);
        System.out.println("	生成原始文件所属位置 : "+realFilePath);
        System.out.println("\n=============================================");
    }

    public static String getFileType(String filePath){
        String realFilePath = "";
        try {
            String fileHeader = getFileHeader(filePath);
            System.out.println("	          fileHeader : "+fileHeader.toUpperCase());
            if (FileTypeConstants.OFFICE.equals(fileHeader.toUpperCase())){
                // office格式(doc、xls、ppt)
                String commandType = getCommandType(filePath);
                if (commandType.toUpperCase().contains(FileTypeConstants.COMMAND_TYPE_DOC)){
                    realFilePath = FilenameUtils.getFullPath(filePath)+ FilenameUtils.getBaseName(filePath) + "_real.doc";
                }else if (commandType.toUpperCase().contains(FileTypeConstants.COMMAND_TYPE_XLS)){
                    realFilePath = FilenameUtils.getFullPath(filePath)+ FilenameUtils.getBaseName(filePath) + "_real.xls";
                }else if (commandType.toUpperCase().contains(FileTypeConstants.COMMAND_TYPE_PPT)){
                    realFilePath = FilenameUtils.getFullPath(filePath)+ FilenameUtils.getBaseName(filePath) + "_real.ppt";
                }
            }else if (FileTypeConstants.OFFICEX.equals(fileHeader.toUpperCase()) || FileTypeConstants.OFFICEX_ZIP.equals(fileHeader.toUpperCase())){
                // officex格式(docx、xlsx、pptx)
                realFilePath = getZipType(filePath);
            }else if (FileTypeConstants.PDF1.equals(fileHeader.toUpperCase()) || FileTypeConstants.PDF2.equals(fileHeader.toUpperCase())){
                // pdf
                realFilePath = FilenameUtils.getFullPath(filePath)+ FilenameUtils.getBaseName(filePath) + "_real.pdf";
            }
            // 若真实类型获取成功则生成该类型文件
            if (StringUtils.isNotEmpty(realFilePath)){
                FileUtils.copyFile(new File(filePath),new File(realFilePath));
            }else{
                realFilePath = filePath;
            }
            System.out.println("	      文件真实类型为 : "+FilenameUtils.getExtension(realFilePath));
        } catch (IOException e) {
            e.printStackTrace();
            return realFilePath;
        }
        return realFilePath;
    }

    /**
     * 识别officeX类型文件
     * @param filePath
     * @throws IOException
     */
    public static String getZipType(String filePath) throws IOException {
        File zipFilePath = new File(FilenameUtils.getFullPath(filePath)+ FilenameUtils.getBaseName(filePath) + ".zip");
        FileUtils.copyFile(new File(filePath),zipFilePath);
        String realFilePath = "";
        ZipFile zipFile = new ZipFile(zipFilePath);
        InputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(zipFilePath));
        ZipInputStream zin = new ZipInputStream(bufferedInputStream);
        ZipEntry ze = null;
        while ((ze = zin.getNextEntry()) != null && StringUtils.isEmpty(realFilePath)) {
            String dirName = ze.getName().toUpperCase();
            if(dirName.startsWith(FileTypeConstants.ZIP_TYPE_DOCX)){
                realFilePath = FilenameUtils.getFullPath(filePath)+ FilenameUtils.getBaseName(filePath) + "_real.docx";
            }else if(dirName.startsWith(FileTypeConstants.ZIP_TYPE_XLSX)){
            	realFilePath = FilenameUtils.getFullPath(filePath)+ FilenameUtils.getBaseName(filePath) + "_real.xlsx";
            }else if(dirName.startsWith(FileTypeConstants.ZIP_TYPE_PPTX)){
                realFilePath = FilenameUtils.getFullPath(filePath)+ FilenameUtils.getBaseName(filePath) + "_real.pptx";
            }
        }
        zin.close();
        bufferedInputStream.close();
        zipFile.close();
        zipFilePath.delete();
        return realFilePath;
    }

    /**
     * 执行脚本命令获取office类型文件
     * @param filePath
     * @return
     */
    public static String getCommandType(String filePath){
        BufferedReader in = null;
        String fileType = "";
        try {
            Runtime run = Runtime.getRuntime();
            String[] command = new String[]{"file","-b","-i",filePath};
            Process process = run.exec(command);
            if (process == null) {
                System.out.println("命令执行失败");
                return "";
            }
            in = new BufferedReader(new InputStreamReader(process.getInputStream(),"UTF-8"));
            String line = null;
            while ((line = in.readLine()) != null) {
                int start = line.indexOf("/");
                int end = line.indexOf(";");
                fileType = line.substring(start + 1, end);
                break;
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
        return fileType;
    }

    /**
     * 获取文件头标识
     * @param filePath
     * @return
     */
    private static String getFileHeader(String filePath) {
        File file=new File(filePath);
        FileInputStream is = null;
        String value = null;
        try {
            is = new FileInputStream(file);
            byte[] b = new byte[10];
            is.read(b, 0, b.length);
            value = bytesToHexString(b);
        } catch (Exception e) {
        } finally {
            if(null != is) {
                try {
                    is.close();
                } catch (IOException e) {}
            }
        }
        return value;
    }

    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();
    }

}


/**
 * @Outher zml
 * Date 2020-7-29 14:30:47
 */
public class FileTypeConstants {

    // office格式(doc、xls、ppt)
    public final static String OFFICE = "D0CF11E0A1B11AE10000";

    // officex格式(docx、xlsx、pptx)
    public final static String OFFICEX = "504B0304140006000800";

    // officex格式(docx、xlsx、pptx)
    public final static String OFFICEX_ZIP = "504B03040A0000000000";

    // pdf
    public final static String PDF1 = "255044462D312E350D0A";
    public final static String PDF2 = "255044462D312E340A25";

    // file -b -i 文件路径
    public final static String COMMAND_TYPE_DOC = "WORD";

    public final static String COMMAND_TYPE_XLS = "EXCEL";

    public final static String COMMAND_TYPE_PPT = "POWERPOINT";

    public final static String ZIP_TYPE_DOCX = "WORD";

    public final static String ZIP_TYPE_XLSX = "XL";

    public final static String ZIP_TYPE_PPTX = "PPT";
}

测试结果:

在这里插入图片描述

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、付费专栏及课程。

余额充值