コード例 #1
0
  public static File saveBookToWorkingFolder(Book book, String exporterType) throws IOException {
    Exporter exporter =
        exporterType == null ? Exporters.getExporter() : Exporters.getExporter(exporterType);
    if (exporter == null) {
      throw new IllegalOpArgumentException("can't find exporter " + exporterType);
    }
    String bn = suggestFileName(book);

    String name = FileUtil.getName(bn);
    String ext = FileUtil.getNameExtension(bn);

    if (Strings.isBlank(name)) {
      name = "book";
    }

    // prefix has > 3
    File f = File.createTempFile("temp_" + name, "." + ext, getWorkingFolder());
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(f);
      exporter.export(book, fos);
    } finally {
      if (fos != null) {
        fos.close();
      }
    }
    return f;
  }
コード例 #2
0
  public void save(Book book) throws IOException {
    String savingPath = WebApps.getCurrent().getRealPath("/WEB-INF/books/") + File.separator;
    File targetFile = new File(savingPath + book.getBookName());
    FileOutputStream fos = null;
    try {
      // write to temporary file first to avoid write error damage original file
      File temp = File.createTempFile("temp", targetFile.getName());
      fos = new FileOutputStream(temp);
      Exporters.getExporter().export(book, fos);

      fos.close();
      fos = null;

      copy(temp, targetFile);
      temp.delete();

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (fos != null) fos.close();
    }
  }