JAVA_读取数据库数据并打成ZIP包下载

当时需求:读取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();
    }
}

 

转载于:https://www.cnblogs.com/sanpi/archive/2011/09/02/2164041.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值