/***
* 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();
}
}