<%@ include file="../../pub/_head.jsp" %>
<%@ page import ="java.util.*" %>
<%@ page import ="java.io.*" %>
<%@ page import = "java.io.File"%>
<%@ page import="java.sql.*,java.util.Date,java.text.*,com.ekingstar.zcgl.bean.SystemParameterBeans,com.ekingstar.zcgl.bean.StringSplit"%>
<%@ page import= "javax.naming.* "%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="com.ekingstar.zcgl.bean.*"%>
<%@ page import="sun.jdbc.rowset.CachedRowSet"%>
<jsp:directive.page import="org.apache.commons.lang.StringUtils"/>
<%
String Operation_Code=StringUtils.defaultIfEmpty(request.getParameter("Operation_Code"),"");
String Operation_Wid = StringUtils.defaultIfEmpty(request.getParameter("Operation_Wid"),"");
String flbh = StringUtils.defaultIfEmpty(request.getParameter("flbh"),"");
%>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<title id="app_title">打印合同草稿</title>
<LINK REL="stylesheet" TYPE="text/css" HREF="../../pub/css/templateautogen.css" ID="templateautogen"></LINK>
<link rel="stylesheet" TYPE="text/css" HREF="../../pub/css/default.css">
<script language="javaScript" src="../../pub/js/util.js"></script>
<script language="vbScript" src="../../pub/js/util.vbs"></script>
</head>
<body background="../../pub/images/pagebgcolor.gif">
<form name="Form1" method="post" id="Form1">
<table width='90%' class='tbl_outer'>
<tr>
<td align="center"> <br>
<input class="BUTON" type='button' name='btnClose' id='btnClose' οnclick='window.close()' value='关 闭'>
</tr>
<div id="divInfo" style="DISPLAY: none; COLOR: red; TEXT-ALIGN: center;FONT-SIZE: 16pt">
<br>正在打印,请等待...!
</div>
</table>
<input type="hidden" id="Operation_Code" name="Operation_Code" value="<%=Operation_Code %>">
<input type="hidden" id="Operation_Wid" name="Operation_Wid" value="<%=Operation_Wid %>">
<input type="hidden" id="flbh" name="flbh" value="<%=flbh %>">
</form>
</body>
</html>
<script language="javascript">
var object = document.getElementById("divInfo");
object.style.display="block";
var Operation_Code=document.all.Operation_Code.value;
var Operation_Wid=document.all.Operation_Wid.value;
var flbh=document.all.flbh.value;
window.Form1.action = "sght_printHtfj.jsp?&Operation_Code="+Operation_Code+"&Operation_Wid="+Operation_Wid+"&flbh="+flbh;
window.Form1.submit();
</script>
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*,java.util.*,java.io.*"%>
<%@ page import="test.*"%>
<%@ page import="java.text.SimpleDateFormat,java.util.Date,com.ekingstar.zcgl.bean.*" %>
<%@ page import="com.ekingstar.zcgl.bean.waterPrint.WordInsertPicture,com.ekingstar.zcgl.bean.waterPrint.pdfAddPicture,com.ekingstar.zcgl.bean.waterPrint.PictureAddPicture" %>
<%@ page import="sun.jdbc.rowset.CachedRowSet"%>
<%@page import=" java.io.BufferedOutputStream"%>
<%@ page import="java.util.zip.*"%>
<%@ page import="java.io.File"%>
<%@ page import= "javax.naming.* "%>
<!--递归删除目录中的子目录下 -->
<%! private static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// 目录此时为空,可以删除
return dir.delete();
}
%>
<!-- 压缩代码 -->
<%! public void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{
//如果当前的是文件夹,则进行进一步处理
if(file.isDirectory()){
//得到文件列表信息
File[] files = file.listFiles();
//将文件夹添加到下一级打包目录
// out.putNextEntry(new ZipEntry(dir+"/"));
dir = dir.length() == 0 ? "" : dir +"/";
//循环将文件夹中的文件打包
for(int i = 0 ; i < files.length ; i++){
createCompressedFile(out, files[i], dir + files[i].getName()); //递归处理
}
}
else{ //当前的是文件,打包处理
//文件输入流
FileInputStream fis = new FileInputStream(file);
out.putNextEntry(new ZipEntry(dir));
//进行写操作
int j = 0;
byte[] buffer = new byte[1024];
while((j = fis.read(buffer)) > 0){
out.write(buffer,0,j);
}
//关闭输入流
fis.close();
}
}
%>
<%!
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
%>
<%
SQLDirect sdj = new SQLDirect();
List wordList = new ArrayList();
List pdfList = new ArrayList();
List picList = new ArrayList();
List otherList = new ArrayList();
String zippath =null;
String zip2path =null;
//zip的临时目录
SystemParameterBeans spb= new SystemParameterBeans();
zip2path = spb.getSystemParameterValue("SBWJCFLJ")+"/temp/";
//水印图片的路径
String waterPrintPath=spb.getSystemParameterValue("SBWJCFLJ")+"/modern/printExcel/"+request.getParameter("Operation_Code")+"_SY.jpg";
//判断文件的标志
String mark="";
//取得flbh
String flbh = request.getParameter("flbh");
String wjm = "";
String ywwid = "";
String blobwid = "";
String Operation_Code = request.getParameter("Operation_Code");
if(Operation_Code==null){
Operation_Code = "";
}
String Operation_Wid = request.getParameter("Operation_Wid");
if(Operation_Wid==null){
Operation_Wid = "";
}
//应该在这判断widstr的长度,来决定循环几次
String wids[]=null;
if(Operation_Wid.indexOf(",")!=-1){
wids=Operation_Wid.split(",");
}
//同时选中多行的情况
if(Operation_Wid.indexOf(",")!=-1){
for(int i=0;i<wids.length;i++){
wordList = new ArrayList();
pdfList = new ArrayList();
picList = new ArrayList();
Operation_Wid=wids[i];
if(!"".equals(Operation_Wid)&&Operation_Wid!=null){
String[][] para= new String[4][2];
para[0][0]="String";
para[0][1]=Operation_Wid;
para[1][0]="String";
para[1][1]=Operation_Code;
para[2][0]="String";
para[2][1]=flbh;
CachedRowSet crs = null;
crs = sdj.exeQuerySQLPara("select ywbh,ywwid,wjm,blobwid from zc_ywfj where ywwid=? and ywbh =? and flbh =? ",para);
while(crs.next()){
wjm = crs.getString("wjm");
Operation_Code = crs.getString("ywbh");
Operation_Wid = crs.getString("ywwid");
if(wjm==null){
wjm = "";
}
blobwid = crs.getString("blobwid");
if(blobwid==null){
blobwid = "";
}
String lzh = request.getParameter("lzh");
if(lzh==null||"null".equals(lzh)){
lzh = "";
}
String jdh = request.getParameter("jdh");
if(jdh==null||"null".equals(jdh)){
jdh = "";
}
if(wjm.indexOf("docx")>0||wjm.indexOf("doc")>0){
wordList.add(wjm);
} else if(wjm.indexOf("pdf")>0){
pdfList.add(wjm);
} else if(wjm.indexOf("png")>0||wjm.indexOf("jpg")>0){
picList.add(wjm);
}else{
otherList.add(wjm);
}
}
}
//模板文件的路径
String oldpath = spb.getSystemParameterValue("SBWJCFLJ")+"/annex/"+Operation_Code+"/"+Operation_Wid+"/"+wjm;
//word
String newpath = spb.getSystemParameterValue("SBWJCFLJ")+"/annex/"+Operation_Code+"/"+Operation_Wid+"/";
//zip里面内容的临时目录
zippath = spb.getSystemParameterValue("SBWJCFLJ")+"/temp/SGHT/";
//把不加水印的的文件拷贝到临时路径
File file0 = new File(zippath+Operation_Wid);
file0.mkdirs();
File File =null;
for(int tempOther=0;tempOther<otherList.size();tempOther++){
copyFile(newpath+otherList.get(tempOther),zippath+Operation_Wid +"/"+otherList.get(tempOther));
}
//WORD文档加水印
if(wordList.size()!=0){
WordInsertPicture wordObj = new WordInsertPicture();
for(int j=0;j<wordList.size();j++){
//遍历循环加水印
wordObj.addWaterMark((newpath + File.separator+ wordList.get(j)),zippath+Operation_Wid +"/"+ wordList.get(j), waterPrintPath);
}
}
//PDF文档加水印
if(pdfList.size()!=0){
pdfAddPicture pdfObj = new pdfAddPicture();
for(int x=0;x<pdfList.size();x++){
pdfObj.addWaterImage(waterPrintPath, newpath + pdfList.get(x),zippath+Operation_Wid +"/"+ pdfList.get(x));
}
}
//图片加水印
if(picList.size()!=0){
PictureAddPicture picObj = new PictureAddPicture();
for(int y=0;y<picList.size();y++){
picObj.markImageByIcon(waterPrintPath, newpath + picList.get(y), zippath+Operation_Wid +"/"+ picList.get(y), -45);
}
}
}
}else{
//只选中一条记录的情况
if(!"".equals(Operation_Wid)&&Operation_Wid!=null){
String[][] para= new String[4][2];
para[0][0]="String";
para[0][1]=Operation_Wid;
para[1][0]="String";
para[1][1]=Operation_Code;
para[2][0]="String";
para[2][1]=flbh;
CachedRowSet crs = null;
crs = sdj.exeQuerySQLPara("select ywbh,ywwid,wjm,blobwid from zc_ywfj where ywwid=? and ywbh =? and flbh =? ",para);
while(crs.next()){
wjm = crs.getString("wjm");
Operation_Code = crs.getString("ywbh");
Operation_Wid = crs.getString("ywwid");
if(wjm==null){
wjm = "";
}
blobwid = crs.getString("blobwid");
if(blobwid==null){
blobwid = "";
}
String lzh = request.getParameter("lzh");
if(lzh==null||"null".equals(lzh)){
lzh = "";
}
String jdh = request.getParameter("jdh");
if(jdh==null||"null".equals(jdh)){
jdh = "";
}
if(wjm.indexOf("docx")>0||wjm.indexOf("doc")>0){
wordList.add(wjm);
}
else if(wjm.indexOf("pdf")>0){
pdfList.add(wjm);
}
else if(wjm.indexOf("png")>0||wjm.indexOf("jpg")>0){
picList.add(wjm);
}else{
otherList.add(wjm);
}
}
}
//模板文件的路径
String oldpath = spb.getSystemParameterValue("SBWJCFLJ")+"/annex/"+Operation_Code+"/"+Operation_Wid+"/"+wjm;
//word
String newpath = spb.getSystemParameterValue("SBWJCFLJ")+"/annex/"+Operation_Code+"/"+Operation_Wid+"/";
//zip的临时目录
zippath = spb.getSystemParameterValue("SBWJCFLJ")+"/temp/SGHT/";
File file0 = new File(zippath+Operation_Wid);
file0.mkdirs();
File File =null;
//把不加水印的的文件拷贝到临时路径
for(int tempOther=0;tempOther<otherList.size();tempOther++){
copyFile(newpath+otherList.get(tempOther),zippath+Operation_Wid +"/"+otherList.get(tempOther));
}
//WORD文档加水印
if(wordList.size()!=0){
WordInsertPicture wordObj = new WordInsertPicture();
for(int j=0;j<wordList.size();j++){
//遍历循环加水印
wordObj.addWaterMark((newpath + File.separator+ wordList.get(j)),zippath+Operation_Wid +"/"+ wordList.get(j), waterPrintPath);
}
}
//PDF文档加水印
if(pdfList.size()!=0){
pdfAddPicture pdfObj = new pdfAddPicture();
for(int x=0;x<pdfList.size();x++){
pdfObj.addWaterImage(waterPrintPath, newpath + pdfList.get(x),zippath+Operation_Wid +"/"+ pdfList.get(x));
}
}
//图片加水印
if(picList.size()!=0){
PictureAddPicture picObj = new PictureAddPicture();
for(int y=0;y<picList.size();y++){
picObj.markImageByIcon(waterPrintPath, newpath + picList.get(y), zippath+Operation_Wid +"/"+ picList.get(y), -45);
}
}
}
String zipname=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
//都是在临时目录下的操作 打成压缩包也分成两种情况 只选中一条 直接打
Operation_Wid = request.getParameter("Operation_Wid");
if(Operation_Wid.indexOf(",")==-1){
File f =null;
try {
File folder = new File(zippath+Operation_Wid);
if(folder.isDirectory()) {
File[] fileList=folder.listFiles();
// 压缩文件名
//如果就两个文件 并且有一个是目录,这是只有一个图片的处理情况
if(fileList.length==1){
mark="1";
}
if(fileList.length>1){
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zippath+"/"+zipname+".zip"));
ZipEntry ze = null;
byte[] buf = new byte[1024];
int readLen = 0;
for (int i = 0; i < fileList.length; i++) {
f = fileList[i];
// 创建一个ZipEntry,并设置Name和其它的一些属性
ze = new ZipEntry(f.getName());
ze.setSize(f.length());
ze.setTime(f.lastModified());
// 将ZipEntry加到zos中,再写入实际的文件内容
zos.putNextEntry(ze);
InputStream is = new BufferedInputStream(new FileInputStream(f));
while ((readLen = is.read(buf, 0, 1024)) != -1) {
zos.write(buf, 0, readLen);
}
is.close();
f.delete();
// out.flush();
}
zos.close();}
else {
f = fileList[0];
mark="1";
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//-------下载文件-------
//只有一个文件的时候
OutputStream os = response.getOutputStream();
FileInputStream fis = null;
if(mark=="1"){
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition","attachment; fileName="+CommFunction.toUtf8String(wjm));//定义保存时的中文名称。
fis = new FileInputStream(f);
}else{
response.setContentType("application/zip");
response.addHeader("Content-Disposition","attachment; fileName="+CommFunction.toUtf8String(zipname+".zip"));
fis = new FileInputStream(zippath+zipname+".zip");
}
if(!"".equals(blobwid)){//blobwid不等于空需要去数据库下载该文件
sdj.downLoadBClob(blobwid,os);
}else{
try{
byte[] b = new byte[1024];
int m = 0;
while ( (m = fis.read(b)) > 0 ){
os.write(b, 0, m);
}
fis.close();
os.flush();
os.close();
}catch(Exception e){
os.flush();
os.close();
}
}
out.clear();
out = pageContext.pushBody();
}else{
//同时多个合同下载附件的时候
File f =null;
try {
File folder = new File(zippath);
if(folder.isDirectory()) {
File[] fileList=folder.listFiles();
// 压缩文件名
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip2path+"/"+zipname+".zip"));
createCompressedFile(zos, folder, "");
zos.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//-------下载文件-------
//只有一个文件的时候
OutputStream os = response.getOutputStream();
FileInputStream fis = null;
if(mark=="1"){
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition","attachment; fileName="+CommFunction.toUtf8String(wjm));//定义保存时的中文名称。
fis = new FileInputStream(f);
}else{
response.setContentType("application/zip");
response.addHeader("Content-Disposition","attachment; fileName="+CommFunction.toUtf8String(zipname+".zip"));
fis = new FileInputStream(zip2path+zipname+".zip");
}
if(!"".equals(blobwid)){//blobwid不等于空需要去数据库下载该文件
sdj.downLoadBClob(blobwid,os);
}else{
try{
byte[] b = new byte[1024];
int m = 0;
while ( (m = fis.read(b)) > 0 ){
os.write(b, 0, m);
}
fis.close();
os.flush();
os.close();
}catch(Exception e){
os.flush();
os.close();
}
}
out.clear();
out = pageContext.pushBody();
}
//删除临时文件
File folder2 = new File(zip2path);
deleteDir(folder2);
%>