首先先了解Executorsc创建的4中线程池的使用:
1)newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需 要,可灵活回收空闲线程,若无可回收,则新建线程。
2)newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
3)newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
4)newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
/** * 下载附件 * @param * @return */ @Override public void fuJianDownLoad() { List<Twtpzj_sbxx> list =sbxxMapper.listSbxxIds(RxrcUtil.getCurrentYear()); ExecutorService fixedThreadPool = Executors.newFixedThreadPool(9); for (Twtpzj_sbxx sbxx : list) { final String id = sbxx.getId(); fixedThreadPool.execute(new Runnable() { @Override public void run() { System.out.println("当前线程:"+Thread.currentThread().getName()); System.out.println(id); System.out.println("========================================"); //文件下载 downFiles(id); } }); } fixedThreadPool.shutdown(); }
***** String path ="/downfile/附件/"+sbxx.getName().trim(); //所创建文件目录 File f = new File(path); if(!f.exists()){ f.mkdirs(); //创建目录 } *** List<Map> listFile=(List) maps.get("fileList"); if(listFile.size()>1){ for(int j=0;j<listFile.size();j++){ Map map = listFile.get(j); byte[] buffer = Axis2Util.decode(map.get("fileBase64").toString()); FileOutputStream fos = null; File file = new File(f, (i+1)+"."+dto.getDza016Name()+(j+1)); fos = new FileOutputStream(file); fos.write(buffer, 0, buffer.length); fos.close(); } }else{ for(Map map:listFile){ byte[] buffer = Axis2Util.decode(map.get("fileBase64").toString()); FileOutputStream fos = null; File file = new File(f, (i+1)+"."+dto.getDza016Name().toString()); fos = new FileOutputStream(file); fos.write(buffer, 0, buffer.length); fos.close(); } } -----------------------
前端:
后端实现:
zos.flush()一定要放在for循环外面;不然的话只能压缩出来的里面只有一份文件,并不能多份文件 ze = new ZipEntry(twtpzjSbxx.getName()+"/"+(i+1)+"."+dto.getDza016Name()+"."+map.get("dzg006")); 其中"/"前面为压缩包里面的文件夹名称,最后一个“.” 是要带文件后缀