demo
package com.ssh.gas.action;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.ssh.common.util.DownLoadUrl;
import com.ssh.common.util.DownLoadUrl2;
import com.ssh.common.util.PropertyFactory;
import com.ssh.common.util.ZipUtils;
public class Demo01 {
public static void main(String[] args) throws InterruptedException {
String[] str= {"HK06N124T92100000166;http://sxapi.pc.ehuatai.com:9004/service_platform/sxApp_getPdfEF.action?param=SEswNk4xMjRUOTIxMDAwMDAxNjYtMjJhZDg2YWRhMmMzMTZkNDFmMzc1ZGU0N2QyMzZhNjI=",
"HK06N124T92100000168;http://sxapi.pc.ehuatai.com:9004/service_platform/sxApp_getPdfEF.action?param=SEswNk4xMjRUOTIxMDAwMDAxNjgtMDNhZGMwOWMwMjk5ZGM1YjI1NGZjOGNhYjg5OTM4ZjU=",
};
System.out.println("下载开始时间:"+System.currentTimeMillis());
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 20, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5), new ThreadPoolExecutor.CallerRunsPolicy()) {};
CountDownLatch latch = new CountDownLatch(10);
for (int j = 0; j < str.length; j++) {
try {
String[] split = str[j].split(";");
System.out.println("保单号:"+split[0]+"下载地址:"+split[1]);
DownLoadUrl2 downLoadUrl2 = new DownLoadUrl2(split[1],split[0]+".pdf","E:\\dow",latch);
threadPool.execute(downLoadUrl2);
int activeCount = ((ThreadPoolExecutor)threadPool).getActiveCount();
long completeTaskCount = ((ThreadPoolExecutor)threadPool).getCompletedTaskCount();
int poolSize = ((ThreadPoolExecutor)threadPool).getPoolSize();
long taskCount = ((ThreadPoolExecutor)threadPool).getTaskCount();
long largestPoolSize = ((ThreadPoolExecutor)threadPool).getLargestPoolSize();
long maximumPoolSize = ((ThreadPoolExecutor)threadPool).getMaximumPoolSize();
System.out.println("当前活动的线程数:"+activeCount+"====完成任务线程数:"+completeTaskCount+"====核心线程数:"+poolSize+"====线程池中任务总量:"+taskCount+"过去执行最多的任务数"+largestPoolSize+"线程池中可以存放的最大的线程数"+maximumPoolSize);
int queueSize = threadPool.getQueue().size();
System.out.println("当前排队线程数:"+ queueSize);
} catch (Exception e) {
e.printStackTrace();
}
}
latch.await();
System.out.println("下载结束时间:"+System.currentTimeMillis());
ZipUtils.zipMultiFile("E:\\dow","E:\\dow\\8ad2599d4c654c349cf05e57182f7fef.zip");
System.out.println("下载压缩时间"+System.currentTimeMillis());
}
}
下载url
package com.ssh.common.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.CountDownLatch;
public class DownLoadUrl2 implements Runnable{
public void downLoadByUrl(String urlStr,String fileName,String savePath) throws IOException{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(10*1000);
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
InputStream inputStream = conn.getInputStream();
byte[] getData = readInputStream(inputStream);
File saveDir = new File(savePath);
if(!saveDir.exists()){
saveDir.mkdir();
}
File file = new File(saveDir+File.separator+fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if(fos!=null){
fos.close();
}
if(inputStream!=null){
inputStream.close();
}
System.out.println("info:"+url+" download success");
}
public byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
private String urlStr;
private String fileName;
private String savePath;
private CountDownLatch latch;
public DownLoadUrl2(String split, String string, String string2, CountDownLatch latch) {
this.urlStr=split;
this.fileName=string;
this.savePath=string2;
this.latch=latch;
}
@Override
public void run() {
try {
downLoadByUrl(urlStr,fileName,savePath);
} catch (Exception e) {
e.printStackTrace();
}
this.latch.countDown();
}
public String getUrlStr() {
return urlStr;
}
public void setUrlStr(String urlStr) {
this.urlStr = urlStr;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
压缩文件
package com.ssh.common.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
public static void zipMultiFile(String filepath, String zippath) {
try {
File file = new File(filepath);
File zipFile = new File(zippath);
InputStream input = null;
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; ++i) {
input = new FileInputStream(files[i]);
zipOut.putNextEntry(new ZipEntry(files[i].getName()));
int temp = 0;
while ((temp = input.read()) != -1) {
zipOut.write(temp);
}
input.close();
}
}
zipOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
压缩后如何返回
//返回给客户端
FileDownLoadUtil fileDownLoadUtil=new FileDownLoadUtil();
fileDownLoadUtil.fileDownLoad(request, response,PropertyFactory.getProperty("DownZipPdf")+ File.separator + C_BATCH+".zip");
//删除文件夹
FileUtilsDelete.delete(PropertyFactory.getProperty("DownZipPdf")+ File.separator +C_BATCH);
//删除文件
FileUtilsDelete.delete(PropertyFactory.getProperty("DownZipPdf")+ File.separator +C_BATCH+".zip");
下载返回客户端
package com.ssh.common.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class FileDownLoadUtil {
private Logger log = Logger.getLogger(FileUploadUtil.class);
public void fileDownLoad(HttpServletRequest request,
HttpServletResponse response, String filePath) throws Exception {
if (response != null && request != null && filePath != null
&& !filePath.equals("")) {
final String userAgent = request.getHeader("USER-AGENT");
filePath = URLDecoder.decode(filePath,"UTF-8");
File file = new File(filePath);
if(!file.exists()){
log.info("############ FilePath: "+filePath);
throw new Exception("文件不存在!");
}
if(!file.isFile()){
log.info("############ FilePath: "+filePath);
throw new Exception("非文件类型!");
}
String fileName = file.getName();
if (userAgent.contains("MSIE")) {
fileName = URLEncoder.encode(fileName, "UTF8");
} else if (userAgent.contains("Mozilla")) {
fileName = new String(fileName.getBytes(), "ISO8859-1");
} else {
fileName = URLEncoder.encode(fileName, "UTF8");
}
response.reset();
response.setContentType("application/octet-stream");
response.setHeader("content-disposition", "attachment; filename=\""
+ fileName + "\"");
FileInputStream fis=new FileInputStream(file);
BufferedInputStream buff=new BufferedInputStream(fis);
byte [] ary_byte=new byte[1024];
long k=0;
OutputStream out = response.getOutputStream();
while(k<file.length()){
int j=buff.read(ary_byte,0,1024);
k+=j;
out.write(ary_byte,0,j);
}
if (out != null) {
out.flush();
out.close();
fis.close();
buff.close();
}
log.info("文件下载完毕!");
} else {
new NullPointerException(
"HttpServletRequest Or HttpServletResponse Or fileName Is Null !");
}
}
}