예제 #1
0
  public static State saveFileByInputStream(InputStream is, String path) {
    State state = null;

    File tmpFile = getTmpFile();

    byte[] dataBuf = new byte[2048];
    BufferedInputStream bis = new BufferedInputStream(is, 8192);
    try {
      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tmpFile), 8192);

      int count = 0;
      while ((count = bis.read(dataBuf)) != -1) {
        bos.write(dataBuf, 0, count);
      }
      bos.flush();
      bos.close();

      state = saveTmpFile(tmpFile, path);

      if (!state.isSuccess()) {
        tmpFile.delete();
      }

      return state;
    } catch (IOException localIOException) {
      log.error(localIOException);
    }
    return new BaseState(false, 4);
  }
예제 #2
0
  /**
   * 保存临时文件到指定目录
   *
   * @author adli 2015年7月19日 下午6:00:33
   * @param tmpFile
   * @param path
   * @return
   */
  private static State saveTmpFile(File tmpFile, String path) {
    State state = null;
    File targetFile = new File(path);

    if (targetFile.canWrite()) return new BaseState(false, 2);
    try {
      FileUtils.moveFile(tmpFile, targetFile);
    } catch (IOException e) {
      return new BaseState(false, 4);
    }

    state = new BaseState(true);
    state.putInfo("size", targetFile.length());
    state.putInfo("title", targetFile.getName());

    return state;
  }
예제 #3
0
  /**
   * 将字节数组作为二进制文件保存到指定文件中
   *
   * @author adli 2015年7月19日 下午6:07:29
   * @param data
   * @param path
   * @return
   */
  public static State saveBinaryFile(byte[] data, String path) {
    File file = new File(path);

    State state = valid(file);

    if (!state.isSuccess()) {
      return state;
    }
    try {
      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
      bos.write(data);
      bos.flush();
      bos.close();
    } catch (IOException ioe) {
      log.error(ioe);
      return new BaseState(false, 4);
    }

    state = new BaseState(true, file.getAbsolutePath());
    state.putInfo("size", data.length);
    state.putInfo("title", file.getName());
    return state;
  }