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); }
/** * 将字节数组作为二进制文件保存到指定文件中 * * @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; }