JAVA_读取数据库数据并打成ZIP包下载
2011-09-02 17:57 三皮开发时 阅读(1226) 评论(0) 编辑 收藏 举报当时需求:读取ORCLE大字段数据,并根据需求将三个大字段的内容分别保存为xml/html文件,并打包下载
package com.comtop.product.taskform.catalogmanagement.action;
import javax.servlet.http.HttpServlet;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import com.comtop.util.Toolkit;
import com.comtop.eaiform.TemplateInfo;
import com.comtop.product.taskform.delegate.TemplateMan;
/**
* 作业表单ZIP包下载servlet
* @author 周波
* @since JDK1.4
* @History 2011年9月2日15:35:49 周波 新建
*/
public class DownTaskFormTemplate extends HttpServlet{
/** 日志对象*/
private final Logger logger = Toolkit.getLogger(this.getClass().getName());
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
downloadTaskFrom(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
}
/*
*下载作业表单ZIP包
*
* */
public void downloadTaskFrom(HttpServletRequest request, HttpServletResponse
response)throws ServletException, IOException{
//模板版本ID
String template_version_id = request.getParameter("template_version_id");
//ZIP包名
String tempalteName = request.getParameter("template_Name");
ZipOutputStream zipOut = null;
InputStream ins = null;
try {
response.reset();
//下载zip文件名
String strZipFileName = tempalteName+".zip";
//得到输出流
response.setHeader("Content-Disposition:",
"attachment;filename=" +new String(strZipFileName.getBytes("gb2312"), "ISO8859_1"));
String path = this.getServletContext().getRealPath("");
StringBuffer sbPath = new StringBuffer(path);
sbPath.append("/temp/");
//2011-5-10 确认是否存在temp文件夹,若不存在则先创建
this.checkAndCreatePacakage(sbPath.toString());
sbPath.append(strZipFileName);
FileOutputStream objFileOutputStream=new FileOutputStream(new File(sbPath.toString()));
zipOut = new ZipOutputStream(objFileOutputStream);
TemplateMan objTemplateMan=new TemplateMan();
String[] files=objTemplateMan.getReportFile(template_version_id);
//写入XML存储格式
if(files[0]!=null){
writeXmlFile(zipOut,files[0],tempalteName+"存储格式.xml");
}
//写入XML模板内容
if(files[1]!=null){
writeXmlFile(zipOut,files[1],tempalteName+"模板内容.xml");
}
//写入HTML内容
if(files[2]!=null){
writeXmlFile(zipOut, files[2],tempalteName+"HTML内容.html");
}
//下载标准模板
// newDownStandarTemplate(packageId,zipOut);
//关闭ZIP流
if (zipOut != null) {
zipOut.close();
}
writeOutStream(sbPath.toString(),response);
}catch (Exception e) {
logger.error("action.DownTaskFormTemplate.下载作业表单ZIP包出错!",e);
}
}
private void writeXmlFile(ZipOutputStream zipOut,String file,String filwName){
try{
zipOut.putNextEntry(new ZipEntry(filwName));
//转换为ZIP文件
zipOut.write(file.getBytes("UTF-8"), 0, file.getBytes("UTF-8").length);
// 关闭ZIP输出流
zipOut.closeEntry();
}catch(Exception e){
logger.error("文件写入异常",e);
}
}
/**
* 2011-09-02 周波 创建 确认是否存在文件夹,若不存在则创建
* @param filePath
*/
private void checkAndCreatePacakage(String filePath){
File objFile = new File(filePath);
if(!objFile.isDirectory()){
objFile.mkdirs();
}
}
//输出流
private void writeOutStream(String fileName,HttpServletResponse response)throws ServletException, IOException{
OutputStream out = response.getOutputStream();
File objFile=new File(fileName);
FileInputStream objFileInputStream=new FileInputStream(objFile);
//定义缓存输入流
BufferedInputStream objBufferedInputStream = new BufferedInputStream(objFileInputStream);
//缓存区
byte[] buffer = new byte[5000];
while (true) {
// 读入缓存输入流
int bytesRead = objFileInputStream.read(buffer, 0, buffer.length);
if (bytesRead < 0) {
break;
}
// 写入输出流
out.write(buffer, 0, bytesRead);
}
// 关闭缓存输入流
objBufferedInputStream.close();
// 关闭文件输入流
objFileInputStream.close();
// 刷新和关闭输出流
out.flush();
out.close();
//将临时文件删除
objFile.delete();
}
}
import javax.servlet.http.HttpServlet;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import com.comtop.util.Toolkit;
import com.comtop.eaiform.TemplateInfo;
import com.comtop.product.taskform.delegate.TemplateMan;
/**
* 作业表单ZIP包下载servlet
* @author 周波
* @since JDK1.4
* @History 2011年9月2日15:35:49 周波 新建
*/
public class DownTaskFormTemplate extends HttpServlet{
/** 日志对象*/
private final Logger logger = Toolkit.getLogger(this.getClass().getName());
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
downloadTaskFrom(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
}
/*
*下载作业表单ZIP包
*
* */
public void downloadTaskFrom(HttpServletRequest request, HttpServletResponse
response)throws ServletException, IOException{
//模板版本ID
String template_version_id = request.getParameter("template_version_id");
//ZIP包名
String tempalteName = request.getParameter("template_Name");
ZipOutputStream zipOut = null;
InputStream ins = null;
try {
response.reset();
//下载zip文件名
String strZipFileName = tempalteName+".zip";
//得到输出流
response.setHeader("Content-Disposition:",
"attachment;filename=" +new String(strZipFileName.getBytes("gb2312"), "ISO8859_1"));
String path = this.getServletContext().getRealPath("");
StringBuffer sbPath = new StringBuffer(path);
sbPath.append("/temp/");
//2011-5-10 确认是否存在temp文件夹,若不存在则先创建
this.checkAndCreatePacakage(sbPath.toString());
sbPath.append(strZipFileName);
FileOutputStream objFileOutputStream=new FileOutputStream(new File(sbPath.toString()));
zipOut = new ZipOutputStream(objFileOutputStream);
TemplateMan objTemplateMan=new TemplateMan();
String[] files=objTemplateMan.getReportFile(template_version_id);
//写入XML存储格式
if(files[0]!=null){
writeXmlFile(zipOut,files[0],tempalteName+"存储格式.xml");
}
//写入XML模板内容
if(files[1]!=null){
writeXmlFile(zipOut,files[1],tempalteName+"模板内容.xml");
}
//写入HTML内容
if(files[2]!=null){
writeXmlFile(zipOut, files[2],tempalteName+"HTML内容.html");
}
//下载标准模板
// newDownStandarTemplate(packageId,zipOut);
//关闭ZIP流
if (zipOut != null) {
zipOut.close();
}
writeOutStream(sbPath.toString(),response);
}catch (Exception e) {
logger.error("action.DownTaskFormTemplate.下载作业表单ZIP包出错!",e);
}
}
private void writeXmlFile(ZipOutputStream zipOut,String file,String filwName){
try{
zipOut.putNextEntry(new ZipEntry(filwName));
//转换为ZIP文件
zipOut.write(file.getBytes("UTF-8"), 0, file.getBytes("UTF-8").length);
// 关闭ZIP输出流
zipOut.closeEntry();
}catch(Exception e){
logger.error("文件写入异常",e);
}
}
/**
* 2011-09-02 周波 创建 确认是否存在文件夹,若不存在则创建
* @param filePath
*/
private void checkAndCreatePacakage(String filePath){
File objFile = new File(filePath);
if(!objFile.isDirectory()){
objFile.mkdirs();
}
}
//输出流
private void writeOutStream(String fileName,HttpServletResponse response)throws ServletException, IOException{
OutputStream out = response.getOutputStream();
File objFile=new File(fileName);
FileInputStream objFileInputStream=new FileInputStream(objFile);
//定义缓存输入流
BufferedInputStream objBufferedInputStream = new BufferedInputStream(objFileInputStream);
//缓存区
byte[] buffer = new byte[5000];
while (true) {
// 读入缓存输入流
int bytesRead = objFileInputStream.read(buffer, 0, buffer.length);
if (bytesRead < 0) {
break;
}
// 写入输出流
out.write(buffer, 0, bytesRead);
}
// 关闭缓存输入流
objBufferedInputStream.close();
// 关闭文件输入流
objFileInputStream.close();
// 刷新和关闭输出流
out.flush();
out.close();
//将临时文件删除
objFile.delete();
}
}