zoukankan      html  css  js  c++  java
  • 常用文件转换公共方法

    /**
    * 文件转pdf字节数组
    * @param file
    * @return
    * @throws IOException
    */
    private static byte[] transform(File file) throws IOException {
    String name = file.getName().toLowerCase();
    if (name.endsWith(".pdf")) {
    return FileUtils.readFileToByteArray(file);
    } else if (name.endsWith(".xls")) {
    byte[] excelData = FileUtils.readFileToByteArray(file);
    return FileTranslateTools.getInstance().excelToPdf(excelData, false);
    } else if (name.endsWith(".doc")) {
    WordToPdfUtils utils = new WordToPdfUtils();
    return utils.wordToPdf(FileUtils.openInputStream(file));
    } else {
    throw new AppException(file.getName() + "文件类型不正确,必须上传 pdf、xls、doc中的一种。");
    }
    }

    /**
    * 将字节写入pdf临时文件
    *
    * @param other1Byte
    * @return
    * @throws IOException
    */
    private static File tFile(byte[] other1Byte) throws IOException {
    String dir = System.getProperty("java.io.tmpdir");
    File f = new File(FilenameUtils.concat(dir, System.currentTimeMillis() + ".pdf"));//默认的临时文件路径
    FileUtils.writeByteArrayToFile(f, other1Byte);
    return f;
    }

  • 相关阅读:
    Partition算法及其应用
    [LeetCode 405.] Convert a Number to Hexadecimal
    LeetCode Path Sum 系列
    组合数计算
    (一) go 实现的基于REST的Web服务
    (十)原型模式
    (一)排序
    (九)装饰模式
    (八)适配器模式
    (七)外观模式
  • 原文地址:https://www.cnblogs.com/tiansan/p/7346049.html
Copyright © 2011-2022 走看看