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

  • 相关阅读:
    Linux終端一行命令发送邮件
    团队冲刺2.4
    团队冲刺2.3
    团队冲刺2.2
    找水王
    评价win10自带输入法——微软拼音输入法
    梦断代码阅读笔记01
    团队冲刺2.1
    第十三周总结
    第十二周总结
  • 原文地址:https://www.cnblogs.com/tiansan/p/7346049.html
Copyright © 2011-2022 走看看