Welcome to Yumao′s Blog.
本po文會給出剩下所有的需求源碼
先是關於HttpClient多線程處理
可以使用listener加載形式
在Tomcat啓動時就進行初始化加載
操作如下:
在web.xml中添加
1 2 3 4 | <!-- 初始化多线程HTTP --> < listener > < listener-class >name.yumao.oss.startup.StartThreadHttpClient</ listener-class > </ listener > |
然後新建name.yumao.oss.startup.StartThreadHttpClient類
源碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | 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 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 = 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 ); } } |