package com.milanoo.wms; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.lang.StringUtils; /** * @author wzw * @version 创建时间:2016年1月8日 下午3:04:08 下载日志,并合并 */ public class DownLog { public final static String logsPath = "D:/log/logs.log"; public static void main(String[] args) throws IOException { Map<String, String[]> map = new HashMap<String, String[]>(); String logs1[] = { "wms-api.log","wms-api.2016-01-10.0.log.zip","wms-api.2016-01-09.0.log.zip" }; String logs2[] = { "wms-all.2016-01-10.0.log.zip" }; map.put("wms/32/", logs1); map.put("wms/35/", logs2); try { mergeLog("http://10.20.20.43:28465/", map); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 说明:下载日志并合并 * * @author wzw * @version 创建时间:2016年1月11日 下午5:53:44 * @param url 日志所在服务器ip及端口 ;例http://10.20.20.43:28465/ * @param projectPos 项目及所在服务器 {"wms/32/","wms/35/","ordercenter/32/"} * @param logs 日志名 "wms-pcApi.2016-01-12.5.log.zip", * "wms-pcApi.2016-01-12.6.log.zip" * @throws IOException */ public static void mergeLog(String url, Map<String, String[]> map) throws IOException { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss"); String newUrl =url; for (String projectPo : map.keySet()) { newUrl = null; newUrl = url; newUrl += projectPo; String logs[] = map.get(projectPo); String newUrl1 =newUrl; for (String string : logs) { newUrl1 = null; newUrl1 = newUrl; newUrl1 += string; if(string.lastIndexOf("zip")!=-1){ InputStream body = get(newUrl1); String path = "D:/log/"+dateFormat.format(new Date())+".zip"; dowload(path, body); try { unzip(path,"D:/log",false); FileWriter fw = new FileWriter(logsPath,true); InputStreamReader isr = new InputStreamReader(new FileInputStream("D:/log/"+string.substring(0,string.lastIndexOf("zip"))), "UTF-8"); BufferedReader bf= new BufferedReader(isr); String line = null; while((line = bf.readLine())!=null){ fw.write(line+"\r\n"); } fw.flush(); fw.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ InputStream body = get(newUrl1); String path = "D:/log/"+string; dowload(path, body); FileWriter fw = new FileWriter(logsPath); InputStreamReader isr = new InputStreamReader(new FileInputStream("D:/log/"+string), "UTF-8"); BufferedReader bf= new BufferedReader(isr); String line = null; while((line = bf.readLine())!=null){ fw.write(line+"\r\n"); } fw.flush(); fw.close(); } } } } /** * 文件下载 * * @param filePath * @param bodyStream * @return * @throws IOException */ public static int dowload(String filePath, InputStream bodyStream) throws IOException { InputStream is = bodyStream; File file = new File(filePath); file.getParentFile().mkdirs(); FileOutputStream fileout = new FileOutputStream(file); /** * 根据实际运行效果 设置缓冲区大小 */ byte[] buffer = new byte[10 * 1024]; int ch = 0; while ((ch = is.read(buffer)) != -1) { fileout.write(buffer, 0, ch); } fileout.flush(); fileout.close(); is.close(); return 1; } @SuppressWarnings("unchecked") public static void unzip(String zipFilePath, String unzipFilePath, boolean includeZipFileName) throws Exception { File zipFile = new File(zipFilePath); //如果解压后的文件保存路径包含压缩文件的文件名,则追加该文件名到解压路径 if (includeZipFileName) { String fileName = zipFile.getName(); if (StringUtils.isNotEmpty(fileName)) { fileName = fileName.substring(0, fileName.lastIndexOf(".")); } unzipFilePath = unzipFilePath + File.separator + fileName; } //创建解压缩文件保存的路径 File unzipFileDir = new File(unzipFilePath); if (!unzipFileDir.exists() || !unzipFileDir.isDirectory()) { unzipFileDir.mkdirs(); } //开始解压 ZipEntry entry = null; String entryFilePath = null, entryDirPath = null; File entryFile = null, entryDir = null; int index = 0, count = 0, bufferSize = 1024; byte[] buffer = new byte[bufferSize]; BufferedInputStream bis = null; BufferedOutputStream bos = null; ZipFile zip = new ZipFile(zipFile); Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>)zip.entries(); //循环对压缩包里的每一个文件进行解压 while(entries.hasMoreElements()) { entry = entries.nextElement(); //构建压缩包中一个文件解压后保存的文件全路径 entryFilePath = unzipFilePath + File.separator + entry.getName(); //构建解压后保存的文件夹路径 index = entryFilePath.lastIndexOf(File.separator); if (index != -1) { entryDirPath = entryFilePath.substring(0, index); } else { entryDirPath = ""; } entryDir = new File(entryDirPath); //如果文件夹路径不存在,则创建文件夹 if (!entryDir.exists() || !entryDir.isDirectory()) { entryDir.mkdirs(); } //创建解压文件 entryFile = new File(entryFilePath); if (entryFile.exists()) { //检测文件是否允许删除,如果不允许删除,将会抛出SecurityException SecurityManager securityManager = new SecurityManager(); securityManager.checkDelete(entryFilePath); //删除已存在的目标文件 entryFile.delete(); } //写入文件 bos = new BufferedOutputStream(new FileOutputStream(entryFile)); bis = new BufferedInputStream(zip.getInputStream(entry)); while ((count = bis.read(buffer, 0, bufferSize)) != -1) { bos.write(buffer, 0, count); } bos.flush(); bos.close(); } } /** * get形式请求http资源 * @param encode * @return */ public static InputStream get(String url) { // Create an instance of HttpClient. HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams() .setConnectionTimeout(5000); client.getHttpConnectionManager().getParams().setSoTimeout(20000); client.getHttpConnectionManager().getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); client.getHttpConnectionManager().getParams().setDefaultMaxConnectionsPerHost(32);//very important!! client.getHttpConnectionManager().getParams().setMaxTotalConnections(256);//very important!! // Create a method instance. GetMethod method = new GetMethod(url); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); InputStream result = null; try { int statusCode = client.executeMethod(method); result = method.getResponseBodyAsStream(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } }
httpclient下载文件并压缩打包
猜你喜欢
转载自wzw5433904.iteye.com/blog/2366525
今日推荐
周排行