java动态导出html文件带图片(前端base64传)

String filename = job.getJobName() + "11.docx";
String zipPath = AudaqueContexts.getProperty("dqms.file.dir") + "/temp/"+jobInstanceId+".html";
//File file1 = new File(zipPath);//创建指定目录和文件名称的文件对象



//用于存储html字符串
StringBuilder stringHtml = new StringBuilder();
//输入HTML文件内容
stringHtml.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
stringHtml.append("<head>");
//stringHtml.append("<meta http-equiv='Content-Type' content='text/html;  charset=UTF-8'>");
stringHtml.append("<title>测试报告</title>");
stringHtml.append("</head>");
stringHtml.append("<body>");
stringHtml.append("<div>");
stringHtml.append("<table border=1 >");
stringHtml.append("<tr><td width=\"5%\">序号</td><td width=\"30%\">检测字段</td><td width=\"5%\">中文备注</td><td width=\"30%\">检测指标</td><td width=\"5%\">规则名称</td><td width=\"10%\">规则权重</td></tr>");

for (int i = 0; i < jobReportVo.getReports().size(); i++) {
   for (JobReport report:jobReportVo.getReports()) {
      stringHtml.append("<tr><td width=\"5%\">"+i+1+"</td><td width=\"35%\">"+report.getRule().getColumnName()+"</td><td width=\"5%\">"+report.getRule().getColumnRemark()+"</td><td width=\"30%\">"+report.getRule().getRuleCategoryName()+"</td><td width=\"5%\">"+report.getRule().getRuleName()+"%</td><td width=\"10%\">"+report.getFineCount()+"</td></tr>");
   }

}
stringHtml.append("</table>");
stringHtml.append("</div>");
stringHtml.append("<div>");
stringHtml.append("<img src=\""+image1+"\" width=\"165\" height=\"60\" />");

      stringHtml.append("</div>");
stringHtml.append("</body></html>");
try{
   File f = new File(zipPath);
   if (f.getParentFile() != null || !f.getParentFile().isDirectory()) {
      // 创建文件
      f.getParentFile().mkdirs();
   }
   //将HTML文件内容写入文件中
   FileOutputStream fileOutputStream=new FileOutputStream(zipPath);
   PrintStream printStream=new PrintStream(fileOutputStream);
   // 转码,转化为utf-8
   String htmls = new String(stringHtml.toString().getBytes("utf-8"),"utf-8");
   printStream.println(htmls);
   fileOutputStream.flush();
   printStream.flush();
   fileOutputStream.close();
   printStream.close();


}catch (Exception e) {
   e.printStackTrace();
}


file = new File(zipPath);
InputStream inStream =null;
// 设置输出的格式
String fileName = "报告";
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
try {
   inStream = new FileInputStream(zipPath);// 文件的存放路径
   response.setContentType("text/html; charset=utf-8");
   response.setCharacterEncoding("utf-8");
   // 这里URLEncoder.encode可以防止中文乱码
   response.setHeader("Content-disposition",
         "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".html");
   while ((len = inStream.read(b)) > 0) {
      response.getOutputStream().write(b, 0, len);
   }
   inStream.close();
   file.delete();
} catch (IOException e) {
   e.printStackTrace();
} finally {
   try {
      if (inStream != null) {
         inStream.close();
      }
   } catch (Exception e2) {
      log.error("关闭流出现异常", e2);
   }
}

中文乱码需要再调一下

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值