java 生成word封面

package com.cnki.commons.servlet;


import java.awt.Color;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.rtf.style.RtfFont;






public class WordCoverServlet extends BaseServlet{

private static final long serialVersionUID = 1L;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String text;
String fields;



private static String paths=Constants.getWEBINFPath().substring(1)+"/downdbf/";//获取项目根目录

protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

String sn = request.getParameter("sn");
Calendar cal = Calendar.getInstance();
String temp = getTimeStringForZipName(cal);
String fn = temp+sn+".doc";
String zipfn = "word_"+temp+".zip";
System.out.println(fn);
try {
resultSetToWord("","","","","");//要输入的参数
System.out.println(paths+fn);
while(!(new File(paths+fn).exists())) {
int i=0;
while(i<10000) {
i++;
}
}
writzip(paths,fn,zipfn);
} catch (Exception e) {
e.printStackTrace();
}

response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename="+new String((zipfn).getBytes(), "ISO-8859-1"));
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(paths+zipfn));
OutputStream out=response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);


byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
} catch (Exception e) {
if(e.toString().indexOf("java.net.SocketException") != -1){
}else{
e.printStackTrace();
}
} finally {
File filezip = new File(paths+zipfn);
if(filezip.exists()) {
filezip.delete();
}
File filedoc = new File(paths+fn);
if(filedoc.exists()) {
filedoc.delete();
}
}

}

public static Connection getConnection(){  
    Connection conn = null;  
try {
String driver = "";
   String dbName = "";    
   String url = ";  


   String user = "";
   String pwd = "";
       
   Class.forName(driver);
conn = DriverManager.getConnection(url, user, pwd);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}



public static void resultSetToWord(String conferenceName,String conferencePosition,String conferenceTime, String collectionUnit,String fn) throws Exception {

String underTitle = null;
if(null != conferencePosition) {
underTitle = "(" + conferencePosition.trim() +"    "+ conferenceTime.trim() +")";
} else {
underTitle = "(" + "  " +"    "+ conferenceTime.trim() +")";
}

String readyTitle = conferenceName;


String scollectionUnit = null;
if(collectionUnit != null && !collectionUnit.equals("")) {
scollectionUnit = collectionUnit.replaceAll("、", "\n");
}


Document document = new Document(PageSize.A4); 
  try {  
   RtfWriter2.getInstance(document,new FileOutputStream(paths+fn));  
 
   document.open();  
   document.setMargins(85f, 85f, 72f, 72f);


   RtfFont font1243 =new RtfFont("黑体", 36, Font.BOLD, Color.BLACK);
   Paragraph p1 = new Paragraph(readyTitle, font1243 );
   p1.setSpacingBefore(48);
   p1.setAlignment(1);  
   document.add(p1);  
   
   RtfFont font123 =new RtfFont("宋体", 22, Font.NORMAL, Color.BLACK);
   Paragraph p2 = new Paragraph(" \n\n",font123);
   p2.setAlignment(1);
   document.add(p2);


   RtfFont font1r =new RtfFont("楷体_GB2312", 22, Font.NORMAL, Color.BLACK);
   Paragraph p3 = new Paragraph(underTitle,font1r);
   p3.setAlignment(1);
   document.add(p3);
   
   Paragraph p4 = new Paragraph(" \n\n\n\n\n\n\n",font123);
   p4.setAlignment(1);
   document.add(p4);
   
   RtfFont font1d =new RtfFont("黑体", 18, Font.NORMAL, Color.BLACK);
   Paragraph p5 = new Paragraph(scollectionUnit,font1d);
   p5.setAlignment(1);
   document.add(p5);
   
   document.close();  
  } catch (FileNotFoundException e) {  
   e.printStackTrace();  
  } catch (DocumentException e) {  
   e.printStackTrace();  
  } catch (Exception e) {  
   e.printStackTrace();  
  }
}

public static String getTimeStringForZipName(java.util.Calendar cal) {
        if (cal != null) {
            SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            java.util.Date theDate = cal.getTime();
            return dateFormatter.format(theDate);
        } else
            return "";
    }

public static void writzip(String filePath,String fn,String zipfn) throws IOException{
   
OutputStream os = new BufferedOutputStream(new FileOutputStream(filePath+"\\"+zipfn));
ZipOutputStream zos = new ZipOutputStream(os);
byte[] buf = new byte[8192];
int len;

File file = new File(filePath+"\\"+fn); 
ZipEntry ze = new ZipEntry(fn); 
zos.putNextEntry(ze);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); 
while ((len = bis.read(buf)) > 0 ) { 
zos.write( buf, 0, len ); 
}
bis.close();
zos.closeEntry();
zos.close();

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值