package com.fr.web.fun;
import com.fr.base.Utils;
import com.fr.cache.Attachment;
import com.fr.stable.fun.AttachmentDownloader;
import com.fr.cache.AttachmentSource;
import com.fr.general.ComparatorUtils;
import com.fr.stable.fun.impl.AbstractAttachmentDownloader;
import com.fr.web.Browser;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
/**
* @author : richie
* @since : 8.0
* 默认的下载处理,反射加载
*/
public class DefaultAttachmentDownloader extends AbstractAttachmentDownloader {
public static DefaultAttachmentDownloader instance = null;
public synchronized static DefaultAttachmentDownloader getInstance() {
if (instance == null) {
instance = new DefaultAttachmentDownloader();
}
return instance;
}
private DefaultAttachmentDownloader() {
}
/**
* 生成下载附件的JavaScript脚本
* @param downloadURL 附件的下载地址
* @return 下载附件的JavaScript脚本
*/
public String createDownloadScript(String downloadURL) {
return "window.open('" + downloadURL + "')";
}
/**
* 下载附件,根据不同的下载方式可以有不同的实现
* @param req HTTP请求
* @param res HTTP响应
* @param id 附件id
* @param idArr 附件id组成的数组
* @throws Exception 如果下载附件异常则抛出此异常
*/
public void download(HttpServletRequest req, HttpServletResponse res, String id, String[] idArr) throws Exception {
if(idArr.length == 1) {
downloadFile(req, res, id);
} else if (idArr.length > 1) {
downloadFileArr(req, res, idArr);
}
}
private void downloadFile(HttpServletRequest req, HttpServletResponse res, String id) throws Exception{
Attachment attach = AttachmentSource.getAttachment(id);
if (attach != null) {
byte[] bytes = attach.getBytes();
res.setHeader("Pragma", "No-cache");
res.setHeader("Cache-Control", "max-age=180");
String reportName = attach.getFilename();
reportName = reportName.trim();
res.setHeader("Content-disposition", "attachment; filename=" + Browser.resolve(req).getEncodedFileName4Download(reportName));
reportName = reportName.toLowerCase();
if (reportName.endsWith(".pdf")) {
res.setContentType("application/pdf");
res.setHeader("extension", "pdf");
} else if (reportName.endsWith(".xls")) {
res.setContentType("application/x-excel");
res.setHeader("extension", "xls");
} else if (reportName.endsWith(".doc")) {
res.setContentType("application/msword");
res.setHeader("extension", "doc");
} else if (reportName.endsWith(".svg")) {
res.setContentType("image/svg+xml");
res.setHeader("extension", "svg");
} else if (reportName.endsWith(".csv")) {
res.setContentType("application/octet-stream");
res.setHeader("extension", "csv");
} else if (reportName.endsWith(".txt")){
res.setContentType("application/octet-stream");
res.setHeader("extension", "txt");
} else {
res.setContentType("application/x-msdownload");
}
OutputStream out = res.getOutputStream();
out.write(bytes);
out.flush();
out.close();
}
}
private void downloadFileArr(HttpServletRequest req, HttpServletResponse res, String[] id_array) throws Exception{
res.setHeader("Pragma", "No-cache");
res.setHeader("Cache-Control", "max-age=180");
res.setHeader("Content-disposition", "attachment; filename=" + "total.zip");
res.setContentType("application/x-msdownload");
OutputStream res_out = res.getOutputStream();
ZipOutputStream out = new ZipOutputStream(res_out);
out.setEncoding("GBK");
for(int i = 0; i < id_array.length; i ++) {
String id = id_array[i];
Attachment attach = AttachmentSource.getAttachment(id);
// shoc:处理文件重名问题,否则会导致zip文件打不开
String newFileName = attach.getFilename();
int duplicateNum = 0;
boolean plusNameType = false;
String dupPreAttName = "";
for(int j = 0; j < i; j++){
Attachment attachPre = AttachmentSource.getAttachment(id_array[j]);
String preAttName = attachPre.getFilename();
if(ComparatorUtils.equals(newFileName, preAttName)){
duplicateNum ++;
dupPreAttName = preAttName;
plusNameType = preAttName.indexOf('(') != -1 && preAttName.indexOf(')') != -1
&& preAttName.lastIndexOf('(') < preAttName.lastIndexOf(')')
&& Utils.isNumeric(preAttName.substring(preAttName.lastIndexOf('('), preAttName.lastIndexOf(')')));
}
}
if(duplicateNum > 0){
if(plusNameType){
newFileName = dupPreAttName.substring(0,dupPreAttName.lastIndexOf('(') + 1) +
(Integer.parseInt(dupPreAttName.substring(dupPreAttName.lastIndexOf('(') + 1, dupPreAttName.lastIndexOf(')')),10) + duplicateNum) +
dupPreAttName.substring(dupPreAttName.lastIndexOf(')'),dupPreAttName.length());
}
else{
newFileName = dupPreAttName.substring(0,dupPreAttName.lastIndexOf('.')) + "("+duplicateNum+")" +
dupPreAttName.substring(dupPreAttName.lastIndexOf('.'), dupPreAttName.length());
}
}
attach.setFilename(newFileName);
if (attach != null) {
byte[] bytes = attach.getBytes();
out.putNextEntry(new ZipEntry(attach.getFilename()));
out.write(bytes);
}
}
out.flush();
out.close();
}
}