java 批量文件下载_java 文件 批量下载,多个文件用压缩包实现

/***

* obtain outputstream

*

* @param uploadifyFile 文件对象

* @param out

* @throws Exception

*/

public void beachDownloadUploadify(List uploadifyFiles, OutputStream out) {

if (UtilGet.listIsEmpty(uploadifyFiles)) {

// change UploadifyFile to outputstream

Date currentTime = new Date();

SimpleDateFormat formatter = new SimpleDateFormat(Constants.FORMATNAME);

Random r = new Random();

String time =formatter.format(currentTime);

String tempMulit = Constants.TEMP_DOWNLOAD + time + r.nextDouble()+r.nextLong()+r.nextLong()+r.nextInt();//临时文件目录(做压缩文件用)

String mulitPath = tempMulit+Constants.ZIPFILE;//压缩文件目录

File tempDownload = new File(tempMulit);

if (!tempDownload.exists() || !tempDownload.isDirectory()) {

tempDownload.mkdirs();

}

try {

File zipFiel = new File(mulitPath);

ZipArchiveOutputStream zipOutput = new ZipArchiveOutputStream(zipFiel);

zipOutput.setEncoding("GBK");

//将文件写入压缩文件夹

for (UploadifyFile uploadifyFile : uploadifyFiles) {

String filePath = Constants.BASEPATH + Constants.SEP + uploadifyFile.getFilePath() + Constants.SEP

+ uploadifyFile.getRegisterName() + uploadifyFile.getRegisterExt();

File inputFile = new File(filePath);

zipOutput.putArchiveEntry(new ZipArchiveEntry(uploadifyFile.getOriginalFileFullName()));

FileInputStream in = new FileInputStream(inputFile);

int b;

while ((b = in.read()) != -1) {

zipOutput.write(b);

}

in.close();

}

zipOutput.closeArchiveEntry();

zipOutput.close();

if (null != tempDownload && tempDownload.isDirectory()) {

FileUtils.deleteDirectory(tempDownload);

}

File file = new File(mulitPath);

FileInputStream fis = new FileInputStream(file);

BufferedInputStream buff = new BufferedInputStream(fis);

byte[] b = new byte[10240];

long k = 0;

while (k < file.length()) {

int j = buff.read(b, 0, 10240);

k += j;

out.write(b, 0, j);

}

out.flush();

if (null != buff) {

buff.close();

}

if (null != out) {

out.close();

}

if(null!=zipFiel&&zipFiel.isFile()){

FileUtils.deleteQuietly(zipFiel);

}

} catch (Exception e) {

log.warn(e);

}

}

}

组装文件对象:

@Override

@SuppressWarnings("unchecked")

public void beachFileDownload(String ruleStr, OutputStream out) throws Exception {

String hqlTemp = StringUtils.replace(ruleStr, ";", "','");

List list = (List) baseManager.getAll(" requireFileCode in ('" + hqlTemp + "') ");

List uploadifies = new ArrayList();

if (UtilGet.isCheckList(list)) {

for (AttachFile attachFile : list) {

if (null != attachFile) {

UploadifyFile uploadifyFile = new UploadifyFile();

uploadifyFile.setFileCode(attachFile.getRequireFileCode());

uploadifyFile.setRegisterName(StringUtils.substring(attachFile.getPhysicalFile(), 0,

StringUtils.lastIndexOf(attachFile.getPhysicalFile(), ".")));

uploadifyFile.setRegisterExt(StringUtils.substring(attachFile.getPhysicalFile(),

StringUtils.lastIndexOf(attachFile.getPhysicalFile(), ".")));

uploadifyFile.setFilePath(StringUtils.substring(attachFile.getFileUrl(), 0,

StringUtils.lastIndexOf(attachFile.getFileUrl(), attachFile.getPhysicalFile()) - 1));

uploadifyFile.setOriginalFileFullName(attachFile.getFileName());

uploadifies.add(uploadifyFile);

}

}

DownloadUploadifyUtil downloadUploadifyUtil = new DownloadUploadifyUtil();

downloadUploadifyUtil.beachDownloadUploadify(uploadifies, out);

}

}

controller 层 调用

/****

* 批量下载

* @param attachFileRecord

* @param request

* @param response

*/

@RequestMapping(method =RequestMethod.GET, value = PBDOWNLOAD, params = "method=beathDownload")

public void beachDownload(AttachFileRecord attachFileRecord, HttpServletRequest request, HttpServletResponse response) {

String ids = request.getParameter(ID);

try {

if (StringUtils.isNotBlank(ids)) {

int i = StringUtils.split(ids,";").length;

response.setContentType("text/html; charset=GBK");

String fileName = DownloadUploadifyUtil.strByteGB("档案库文档["+i+"].zip", request);

response.setContentType("application/x-msdownload");

OutputStream out=response.getOutputStream();

response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

attachFileService.beachFileDownload(ids,out);

}

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值