Java tutorial
/* * Project: greenline-biz-manager * * File Created at 2012-3-21 * * Copyright 2012 Greenline.com Corporation Limited. * All rights reserved. * * This software is the confidential and proprietary information of * Greenline Company. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Greenline.com. */ package com.greenline.guahao.biz.manager.storage.impl; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.greenline.guahao.biz.manager.storage.StorageManager; import com.greenline.guahao.biz.manager.storage.StoreResult; import com.greenline.guahao.biz.manager.storage.StoreResult.ResultEnum; /** * @Type DefaultStorageManagerImpl * @Desc ??? * @author weirui.shenwr * @date 2012-4-17 * @Version V1.0 */ public class DefaultStorageManagerImpl implements StorageManager { protected static final Log logger = LogFactory.getLog(DefaultStorageManagerImpl.class); private final int DEFAULT_BUFFER_SIZE = 1024 * 4; // config.properties? private String rootDirectory; public void setRootDirectory(String rootDirectory) { this.rootDirectory = rootDirectory; } public String getRootDirectory() { return rootDirectory; } /** * * * @param path * @return */ public boolean delete(String path) { File file = new File(rootDirectory + path); return file.delete(); } /** * ?? * * @param path ? * @return */ public InputStream getFile(String path) { try { InputStream in = new FileInputStream(rootDirectory + path); return in; } catch (Exception e) { logger.error("?:" + path, e); return null; } } /** * ? */ public void init() { if (!rootDirectory.endsWith("/")) { rootDirectory = rootDirectory + "/"; } File file = new File(rootDirectory); if (!file.exists()) { file.mkdirs(); } } /** * ? * * @param in * @param path * @return */ public StoreResult create(InputStream inputStream, String path) { StoreResult ret = new StoreResult(); BufferedInputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(inputStream); out = new BufferedOutputStream(new FileOutputStream(rootDirectory + path)); ret.setPath(path); int len = 0; byte[] bytes = new byte[DEFAULT_BUFFER_SIZE]; while (-1 != (len = in.read(bytes))) { out.write(bytes, 0, len); } } catch (Exception e) { logger.error(".", e); ret.setResult(ResultEnum.ERROR); ret.setMessage(e.getMessage()); } finally { if (in != null) { try { in.close(); } catch (IOException e) { // ignore } } if (out != null) { try { out.close(); } catch (IOException e) { // ignore } } } return ret; } @Override public boolean rename(String path, String newPath) { File file = new File(rootDirectory + path); File dest = new File(rootDirectory + newPath); return file.renameTo(dest); } @Override public OutputStream getOutputStream(String path) { try { return new FileOutputStream(rootDirectory + path); } catch (Exception e) { logger.error("?outputStream:" + path, e); return null; } } }