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); } }
中文乱码需要再调一下