Welcome to Yumao′s Blog.
阿裏云OSS分享系統設計(二)
, 2013年03月18日 , Java Language , 评论 在〈阿裏云OSS分享系統設計(二)〉中留言功能已關閉 ,

本po文會給出剩下所有的需求源碼
先是關於HttpClient多線程處理
可以使用listener加載形式
在Tomcat啓動時就進行初始化加載
操作如下:

在web.xml中添加

    <!-- 初始化多线程HTTP -->
    <listener>
		<listener-class>name.yumao.oss.startup.StartThreadHttpClient</listener-class>
	</listener>

然後新建name.yumao.oss.startup.StartThreadHttpClient類
源碼如下:

package name.yumao.oss.startup;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import name.yumao.oss.common.ThreadHttpClient;
import org.apache.log4j.Logger;

public class StartThreadHttpClient implements ServletContextListener {
	private static Logger logger = Logger.getLogger(StartThreadHttpClient.class);
	public void contextDestroyed(ServletContextEvent arg0) {
	}
	public void contextInitialized(ServletContextEvent arg0) {
		logger.info("*********初始化多线程Http*********");
		ThreadHttpClient.getInstance();
		ThreadHttpClient.getInstance().initHttp();
		logger.info("*******多线程Http初始化完毕********");
	}
}

ThreadHttpClient多線程類以前的文章有Po過
這裏就不多提了
可以使用部落格右側的檢索功能進行關鍵字檢索

然後是核心action類
因爲懶所以連service都沒分開了
但是又不想改
所以就成如下這樣了
Orz

package name.yumao.oss.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import name.yumao.oss.common.ThreadHttpClient;
import name.yumao.oss.entity.CommonPrefixesBean;
import name.yumao.oss.entity.ContentsBean;
import name.yumao.oss.entity.ListBucketResultBean;
import name.yumao.oss.entity.OssFileBean;
import name.yumao.oss.utils.GMTUtils;
import name.yumao.oss.utils.OSSUtils;
import name.yumao.oss.utils.SignUtils;
import name.yumao.oss.utils.JOXUtils;


public class AliyunOssAction {
    private static final String ACCESS_ID = "";
    private static final String ACCESS_KEY = "";
    private static final String BUCKET_NAME = "";
    private static final String GETURL = "http://oss.aliyuncs.com/" + BUCKET_NAME;
    //---------------------------------------------------------------------
    private List<OssFileBean> ossFileBeanList;
	public List<OssFileBean> getOssFileBeanList() {
		return ossFileBeanList;
	}
	public void setOssFileBeanList(List<OssFileBean> ossFileBeanList) {
		this.ossFileBeanList = ossFileBeanList;
	}
	private String nowUrl;
	public String getNowUrl() {
		return nowUrl;
	}
	public void setNowUrl(String nowUrl) {
		this.nowUrl = nowUrl;
	}
	//---------------------------------------------------------------------
	public String index() throws Exception{
		//初始化列表
		if(ossFileBeanList==null){
			ossFileBeanList = new ArrayList<OssFileBean>();
		}
		//若没有当前路径 则判定为根目录
		if(nowUrl==null){
			nowUrl = "";
		}
		//连接阿里云OSS 获取文件列表
		DefaultHttpClient http=ThreadHttpClient.getInstance().getHttpclient();
		//设置HTTP超时时间
		setTimeout(http);
		//新建Get对象
		HttpGet get = new HttpGet(GETURL);
		//设置get头
		get.setHeader("Date", GMTUtils.getGMTDateStr());
		get.setHeader("max-keys","1000");
		get.setHeader("prefix", new String(nowUrl.getBytes("UTF-8"),"ISO8859-1"));
		get.setHeader("delimiter","/");
		//准备使用密钥进行加密
		String SignStr = "GET\n\n\n"+GMTUtils.getGMTDateStr()+"\n/" + BUCKET_NAME;
		byte[] signature = SignUtils.getSignature(SignStr.getBytes("UTF-8"), ACCESS_KEY.getBytes("UTF-8"));
		String sign = Base64.encode(signature);
		get.setHeader("Authorization","OSS " + ACCESS_ID + ":" + sign);
		//进行数据请求
 		HttpResponse response = http.execute(get);
		String resData = new String(EntityUtils.toString(response.getEntity()).getBytes("ISO8859-1"),"UTF-8");
		//转换成Bean进行处理
		ListBucketResultBean listBucketResultBean = JOXUtils.XML2Bean(resData);
		//进行bean内容的处理以及list的设置与输出
		OssFileBean ossFileBean;
		ContentsBean[] contentsBean = listBucketResultBean.getContents();
		CommonPrefixesBean[] commonPrefixesBean = listBucketResultBean.getCommonPrefixes();
		if(!nowUrl.equals("")){
			ossFileBean = new OssFileBean();
			ossFileBean.setFileName("..");
			if(nowUrl.indexOf("/")==nowUrl.lastIndexOf("/")){
				ossFileBean.setToDo("openFolder('" + nowUrl.substring(nowUrl.indexOf("/")+1) + "')");
			}else{
				ossFileBean.setToDo("openFolder('" +
nowUrl.substring(0,nowUrl.length()-1).substring(
0,nowUrl.substring(0,nowUrl.length()-1).lastIndexOf("/"))+
"/" + "')");
			}
			ossFileBeanList.add(ossFileBean);
		}
		if(commonPrefixesBean!=null){
			for(int i=0;i<commonPrefixesBean.length;i++){
				ossFileBean = new OssFileBean();
				if(nowUrl.equals("")){
					ossFileBean.setFileName(commonPrefixesBean[i].getPrefix());
				}else{
					ossFileBean.setFileName(
					commonPrefixesBean[i].getPrefix().substring(nowUrl.length()));
				}
				ossFileBean.setToDo("openFolder('" + commonPrefixesBean[i].getPrefix() + "')");
				ossFileBean = OSSUtils.FoldersTODO(ossFileBean,commonPrefixesBean[i]);
				ossFileBeanList.add(ossFileBean);
			}
		}
		if(contentsBean!=null){
			for(int i=0;i<contentsBean.length;i++){
				if(!contentsBean[i].getKey().equals(nowUrl)){
					ossFileBean = new OssFileBean();
					if(nowUrl.equals("")){
						ossFileBean.setFileName(
						contentsBean[i].getKey());
					}else{
						ossFileBean.setFileName(
						contentsBean[i].getKey().substring(nowUrl.length()));
					}
					ossFileBean.setSize(contentsBean[i].getSize());
					ossFileBean.setLastModified(
					contentsBean[i].getLastModified().replace("T", " ").replace(".000Z", ""));
					ossFileBean.setToDo("downFile('http://reader.oss.aliyuncs.com/" + contentsBean[i].getKey() + "')");
					ossFileBean = OSSUtils.FilesTODO(ossFileBean,contentsBean[i]);
					ossFileBeanList.add(ossFileBean);
				}
			}
		}
		return "showlist";
	}
	private static void setTimeout(DefaultHttpClient http){
		//设置打开链接超时时间
		http.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
		//设置读取数据超时超时时间
		http.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 15000);
	}
}
关键字:, , ,

评论已关闭