html报表下载

html报表下载。用到了freemarker模板引擎和一个压缩的工具类,熟悉freemarker判断和循环的语法,至于压缩的工具类那就度娘吧。这里再现逻辑过程和一些细节。

 public void downloadSingleHtml(ServletRequest srequest,String jobId, HttpServletRequest request, HttpServletResponse response) throws Exception {
        JobStatistic jobStatistic = jobStatisticService.listJobStatistic(jobId);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        long tm = jobStatistic.getJobBeginTime().getTime();
        String d = format.format(tm);
        runningLogService.logging(srequest.getRemoteHost(),"report_manage", MvcUtils.getCurrentUserName(), "下载html报告:" + jobStatistic.getAssetName() + "_" + d);
        String realPath = request.getSession().getServletContext().getRealPath("/");
        Map data = reportDataService.createOneAssetOneJobReportData(jobId);
     	// 初始化Freemarker配置
        Configuration  config = new Configuration();
        // 设置Freemarker模板文件的位置(这里模板放在report_template文件夹下)
	config.setServletContextForTemplateLoading(request.getSession().getServletContext(), "report_template");
        //获取模板
        Template t =  config.getTemplate("vulnerability.ftl","utf-8");
        t.setEncoding("utf-8");
        //生成文件名(采用资产名字+系统当前系统时间+.html)
        String fileNamePre = jobStatistic.getAssetName().replace(" ", "_").replace(".", "_") + "_" + d;
        long fileNameSuf =System.currentTimeMillis();
        String fileName  = fileNamePre+fileNameSuf+".html";
        //判断服务器上是否有htmldownload文件夹,没有就创建
        File file = new File(realPath+"htmldownload");
        if(!file.exists()&&!file.isDirectory()){
            file.mkdir();
        }
        //生成文件路径和文件名(放在htmldownload下供压缩)
        File fileProduce = new File(realPath+"htmldownload/"+fileName);
        Writer out = null;
        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileProduce),"utf-8"));
        t.process(data, out);
        out.flush();
        out.close(); 
        //把htmldownload文件夹下所有的文件夹和文件都压缩(打包到服务器上)
       //realPath="D:\apache-tomcat-7.0.54\webapps\UnisSystem\"
        ZipCompressor zc = new ZipCompressor(realPath+"/htmldownload/"+fileNamePre+".zip"); 
        zc.compress(realPath+"/static/js",realPath+"/static/style",realPath+"/static/images",realPath+"htmldownload/"+fileName);
        //在客户端进行下载
        InputStream read = null ;
        OutputStream outputStream = null;
        try {
            read = new FileInputStream(realPath+"/htmldownload/"+fileNamePre+".zip");
            String downloadFileName = jobStatistic.getAssetName().replace(" ", "_").replace(".", "_") + "_" + d + ".zip";
            response.setHeader("Content-Disposition", "attachment;filename="+downloadFileName);
            response.setContentType("application/octet-stream; charset=utf-8");
            outputStream = response.getOutputStream();
            byte b[]=new byte[1024];
            while(read.read(b)!=-1){
                outputStream.write(b);
            }
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(read!=null){
               read.close(); 
            }
            if(outputStream!=null){
                outputStream.close();
            }
        }
        //删除生成的html文件和整个压缩包
        fileProduce.delete();
        File fileZip = new File(realPath+"/htmldownload/"+fileNamePre+".zip");
        fileZip.delete();
    }
    

这段代码片段就展现了整个逻辑过程

 

1,首先freemarker的jar包需要导入到项目中,准备好压缩的工具类和需要的模板,利用freemarke模板引擎把去数据库查询到的数据动态插入到模板中,t.process(data, out)其中data的类型是Map,out的类型是Writer。

 

2,然后生成想要文件类型,要考虑生成的文件放在哪儿?这里是放在服务器的一个文件夹下,当时是自己在项目中的webapp下建立的一个文件夹,在自己的环境下是可以实现功能的,测试组那里是不可以的,因为他那儿没有这个文件夹,自然就不可以了,问题找到了,办法就是将自己要下载的内容存在服务器temp文件夹下或者就是利用java语言可以自己判断是否有这个文件或文件夹,如果没有就创建一个,这就解决了。

 

3,最后把事先准备好的js和css以及图片一起压缩打包放在同一个文件下供用户下载,当用户下载完后要注意应该将压缩包删除。

 

整个过程应该解决的问题:

 

一,是否能够利用freemarker准确地插入数据,熟悉freemarker语法和准备好模板?

二,生成的文件和事先准备好的js、css和images应该打包压缩并思考放在哪儿?压缩的工具类是否写正确了?

三,明确路径是什么?文件夹放在的是工程路径还是服务器路径?这里考虑路径是因为要考虑到测试环境和本地环境的差异。

四,是否清晰客户端的下载过程?注意文件下载了后要将其删除掉。

 


 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值