Esempio n. 1
0
  /**
   * 写入图片至文件夹中
   *
   * @param 从Excel中读取的图片对象
   * @return 文件存入成功的完整路径
   */
  public static String writeImageToFile(PictureData pictureData, String fileName) {
    if (null != pictureData) {
      // 数据流
      byte[] byteData = pictureData.getData();

      // 文件后辍
      String ext = pictureData.suggestFileExtension();

      FileOutputStream out = null;
      try {
        // 按日期命名文件夹2014-09-02
        String pathDate = DateUtils.getNow(DateUtils.SIMPLE_DEFAULT_FORMAT);

        // 获取当前工程子目录
        String webPath = ExcelFileUtils.getExcelModelPath(FILE_TEMP_DIR);

        // 判断路径是否存在,存在则直接返回
        String path = mkdir(webPath, pathDate);

        // 存储的文件名
        String tmpFileName =
            "/" + fileName + UNDER_LINE + (System.currentTimeMillis()) + POINT + ext;
        File file = null;
        if (StringUtils.isNotEmpty(path)) {
          file = new File(path + tmpFileName);
          out = new FileOutputStream(file);
          out.write(byteData);
        }

        return null == file ? null : FILE_TEMP_DIR + pathDate + tmpFileName;

      } catch (Exception e) {
        LOG.error(e.getMessage());
      } finally {
        if (null != out) {
          try {
            out.close();
          } catch (IOException e) {
            LOG.error(e.getMessage());
          }
        }
      }
    }
    return null;
  }