private void riskNoticesExport(HttpServletRequest request,
HttpServletResponse response) {
String noticeIds = request.getParameter("noticeIds");
String types = request.getParameter("types");
String[] noticeIdArr = noticeIds.split(";");
String[] typeArr = types.split(";");
File file = getNoticeFileZip(response,noticeIdArr,typeArr);
try {
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
String fileName="11.zip";
//如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("GBK"),"iso8859_1"));
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}finally{
try {
File f = new File(file.getPath());
f.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private File getNoticeFileZip(HttpServletResponse response,String[] noticeIdArr, String[] typeArr) {
String tempFilePath=Application.datFileRootPath+File.separator +Application.weekRiskFilePath+File.separator;
String tempFilePath2 = tempFilePath+"a@@a"+File.separator;
File filewjj = new File(tempFilePath2);
if(!filewjj .exists()){
filewjj .mkdir();
}else{
delAllFile(tempFilePath2);
}
List files = getNoticeFiles(tempFilePath2,noticeIdArr,typeArr);
File file = new File(tempFilePath+"1@@.zip");
try {
if(file.exists()){
file.delete();
file = new File(tempFilePath+"1@@.zip");
}
if (!file.exists()){
file.createNewFile();
}
ZipArchiveOutputStream zos =(ZipArchiveOutputStream) new ArchiveStreamFactory()
.createArchiveOutputStream("zip", new FileOutputStream(file));
zos.setEncoding("gbk");
ZipArchiveEntry ze;
for(File f:files){
if(!f.exists())
continue;
ze =new ZipArchiveEntry(getEntryName(f,tempFilePath2));//获取每个文件相对路径,作为在ZIP中路径
zos.putArchiveEntry(ze);
//folder
if(ze.isDirectory()){
zos.closeArchiveEntry();
continue;
}
//file
FileInputStream fis =new FileInputStream(f);
IOUtils.copy(fis, zos);
fis.close();
zos.closeArchiveEntry();
}
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
private String getEntryName(File f, String rootPath) {
String entryName;
String fPath =f.getAbsolutePath();
if(fPath.indexOf(rootPath)!=-1)
entryName =fPath.substring(rootPath.length()+1);
else
entryName =f.getName();
if(f.isDirectory())
entryName +="/";//"/"后缀表示entry是文件夹
return entryName;
}
/**
* 删除文件夹下所有文件
* @param path
* @return
*/
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
flag = true;
}
}
return flag;
}
private List getNoticeFiles(String tempFilePath, String[] noticeIdArr,
String[] typeArr) {
List files = new ArrayList();
for(int i=0;i
for(int j=0;j
File file = exportNotice(tempFilePath,noticeIdArr[j],typeArr[i],null);
files.add(file);
}
}
return files;
}
private File exportNotice(String tempFilePath, String noticeID, String type, String wordType) {
File file =null;
if(type == null || type.equals("")){
type = "doc";
}
try {
RiskNotice notice = this.noticeService.getRiskNoticeByNoticeID(noticeID);
List replies = this.noticeService.getRiskNoticeReplyByNoticeID(noticeID);
String weekStart = notice.getWeekStart();
String week = getWeekByDate(weekStart);
if(type.equals("doc")){
String ftlPath = this.getServletContext().getRealPath("/ftl/riskNotice");
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File(ftlPath));
Map root = getRiskNoticeFTLData(notice);
boolean noSign = (wordType != null && !wordType.equals("") && wordType.equals("noSign"));
Template t = null;
double width = 100.35;
double height = 35.2;
if(replies == null || replies.size() <= 0){
t = cfg.getTemplate("RiskNotice_1.ftl","utf-8");
root.put("dept1", "部");
root.put("dept1_img", "");
}else{
t = cfg.getTemplate("RiskNotice_"+replies.size()+".ftl","utf-8");
int i = 1;
int size = replies.size();
if(size == 4){
width = 100.35;
height = 35.2;
}else if(size == 5){
width = 90.35;
height = 30.2;
}
for(RiskNoticeReply reply:replies){
root.put("dept"+i, reply.getSignDept());
//byte [] bs = strImgToByte(new String(reply.getSign()));
byte [] bs = reply.getSign();
if(noSign || (bs == null || bs.length <= 0)){
root.put("dept"+i+"_img", "");
}else{
root.put("dept"+i+"_img", getBase64String(cfg,"image_"+i,bs,width,height));
}
i++;
}
}
String publishUnit = notice.getPublishUnit();
if(publishUnit == null || publishUnit == ""){
root.put("publishUnit", "安");
}else{
root.put("publishUnit", publishUnit.trim());
}
double width1 = 120.35;
double height1 = 50.2;
byte [] bs = notice.getSignBZ();
if(noSign || (bs == null || bs.length <= 0)){
root.put("bz", "");
}else{
root.put("bz", getBase64String(cfg,"image_bz",bs,width,height));
}
bs =notice.getSignExamine();
if(noSign || (bs == null || bs.length <= 0)){
root.put("sh", "");
}else{
root.put("sh", getBase64String(cfg,"image_sh",bs,width,height));
}
bs = notice.getSignIssure();
if(noSign || (bs == null || bs.length <= 0)){
root.put("qf", "");
}else{
root.put("qf", getBase64String(cfg,"image_qf",bs,width1,height1));
}
StringWriter oWriter = null;
FileOutputStream stream = null;
String fileName ="";
try {
String []noticeNum = notice.getNoticeNum().split(";");
fileName = ""+noticeNum[0]+"("+week+").doc";
if(noticeNum.length >= 2){
fileName = "华东"+noticeNum[0]+"("+week+").doc";
}
stream = new FileOutputStream(new File(tempFilePath+fileName));
oWriter = new StringWriter();
t.process(root, oWriter);
String s = oWriter.toString();
oWriter.close();
stream.write(s.getBytes("utf-8"));
stream.flush();
} catch (Exception e1) {
e1.printStackTrace();
}finally{
stream.close();
oWriter.close();
file = new File(tempFilePath+fileName);
}
}else if(type.equals("pdf")){
ByteArrayOutputStream os = new ByteArrayOutputStream();
Document document = createPdfDoc(notice,replies,os);
document.close();
FileOutputStream stream= null;
String fileName ="";
try {
String []noticeNum = notice.getNoticeNum().split(";");
fileName = ""+noticeNum[0]+"("+week+").pdf";
if(noticeNum.length >= 2){
fileName = ""+noticeNum[0]+"("+week+").pdf";
}
stream =new FileOutputStream(new File(tempFilePath+fileName));
stream.write(os.toByteArray());
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}finally{
stream.flush();
stream.close();
file = new File(tempFilePath+fileName);
}
}else if(type.equals("image")){
ByteArrayOutputStream os = new ByteArrayOutputStream();
Document d = createPdfDoc(notice,replies,os);
d.close();
PDDocument document = PDDocument.load(os.toByteArray());
PDFRenderer pdfRenderer = new PDFRenderer(document);
BufferedImage bim = pdfRenderer.renderImageWithDPI(0, 图片DPI, ImageType.RGB);
//ImageIOUtil.writeImage(bim, "D:\\"+ (pageCounter++) + ".png", 300);
os.close();
os = new ByteArrayOutputStream();
ImageIO.write(bim, 图片格式, os);
document.close();
FileOutputStream stream= null;
String fileName ="";
try {
String []noticeNum = notice.getNoticeNum().split(";");
fileName = ""+noticeNum[0]+"("+week+").jpg";
if(noticeNum.length >= 2){
fileName = ""+noticeNum[0]+"("+week+").jpg";
}
stream =new FileOutputStream(new File(tempFilePath+fileName));
stream.write(os.toByteArray());
} catch (IOException e1) {
e1.printStackTrace();
}
stream.flush();
stream.close();
file = new File(tempFilePath+fileName);
}
} catch (Exception e) {
e.printStackTrace();
}
return file;
}