freemarker 在生成word 的时候同时动态插入图片

freemarker 在生成word 的时候同时动态插入图片

freemarker 在生成word 的时候同时动态插入图片

 

@RequestMapping(value = "downLoadWord", method = RequestMethod.GET)
public void downloadExcel(String constCheckId, HttpServletResponse response) throws IOException, TemplateException {
     ConstCheck     constCheck=constCheckService.getDownLoadNewSById(constCheckId);
    Group groupSG = groupService.findGroupByGroupId(constCheck.getTaskSGGroupId());
    Group groupJL = groupService.findGroupByGroupId(constCheck.getTaskJLGroupId());
    constCheck.setTaskSGGroupName(groupSG.getGroupName());
    constCheck.setTaskJLGroupName(groupJL.getGroupName());
    if(StringUtils.isBlank(constCheck.getFileRelevanceKey())){
        Map<String, Object> constCheckMap = new HashMap<>();
        if(StringUtils.isBlank(constCheck.getTaskName())){
            constCheckMap.put("task","无项目名称");
        }else{
            constCheckMap.put("task",constCheck.getTaskName());
        }
        if(StringUtils.isBlank(constCheck.getCheckResult())){
            constCheckMap.put("check","无监理工作情况");
        }else{
            constCheckMap.put("check",constCheck.getCheckResult());
        }
        if(StringUtils.isBlank(constCheck.getRemark())){
            constCheckMap.put("remark","无备注");
        }else{
            constCheckMap.put("remark",constCheck.getRemark());
        }
        if(StringUtils.isBlank(constCheck.getTaskSGGroupName())){
            constCheckMap.put("SG","无施工单位");
        }else{
            constCheckMap.put("SG",constCheck.getTaskSGGroupName());
        }
        if(StringUtils.isBlank(constCheck.getTaskJLGroupName())){
            constCheckMap.put("JL","无监理单位");
        }else{
            constCheckMap.put("JL",constCheck.getTaskJLGroupName());
        }
        if(StringUtils.isBlank(constCheck.getCheckLocalDesc())){
            constCheckMap.put("desc","无施工地点");
        }else{
            constCheckMap.put("desc",constCheck.getCheckLocalDesc());
        }
        if(StringUtils.isBlank(constCheck.getUserName())){
            constCheckMap.put("user","无相关记录人");
        }else{
            constCheckMap.put("user",constCheck.getUserName());
        }
        configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
        configuration.setClassForTemplateLoading(this.getClass(), "/templates/pages/domain/pro"); // FTL文件所存在的位置
        Template t=null;
        t = configuration.getTemplate("createReport.ftl");  // 读取相应的ftl 文件
        Writer out = null;
        response.setContentType("application/vnd.ms-excel");
        String fileName = constCheck.getTaskName() + ".doc";
        fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        response.addHeader("Content-Disposition", "attachment;filename="+ fileName);
        out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream()));
        t.process(constCheckMap, out);
        out.close();
    }else{
        Map<String, Object> constCheckMap = new HashMap<>();
        Uploadfile uploadfileQuery = new Uploadfile();
        uploadfileQuery.setFileRelevanceKey(constCheck.getFileRelevanceKey());
        List<Uploadfile>  uploadfileList = uploadfileService.findList(uploadfileQuery);
        List<Map<String, Object>> imgList = new ArrayList<Map<String, Object>>();
        for (Uploadfile uploadfile : uploadfileList) {
            Map<String, Object> map = new HashMap<String, Object>();
            String imageBase = getImageStr2(uploadfile.getPath(),uploadfile.getFileName());
            map.put("image",imageBase);
            imgList.add(map);
        }
        constCheckMap.put("imgList", imgList);
        if(StringUtils.isBlank(constCheck.getTaskName())){
            constCheckMap.put("task","无项目名称");
        }else{
            constCheckMap.put("task",constCheck.getTaskName());
        }

        if(StringUtils.isBlank(constCheck.getCheckResult())){
            constCheckMap.put("check","无监理工作情况");
        }else{
            constCheckMap.put("check",constCheck.getCheckResult());
        }
        if(StringUtils.isBlank(constCheck.getRemark())){
            constCheckMap.put("remark","无备注");
        }else{
            constCheckMap.put("remark",constCheck.getRemark());
        }
        if(StringUtils.isBlank(constCheck.getTaskSGGroupName())){
            constCheckMap.put("SG","无施工单位");
        }else{
            constCheckMap.put("SG",constCheck.getTaskSGGroupName());
        }
        if(StringUtils.isBlank(constCheck.getTaskJLGroupName())){
            constCheckMap.put("JL","无监理单位");
        }else{
            constCheckMap.put("JL",constCheck.getTaskJLGroupName());
        }
        if(StringUtils.isBlank(constCheck.getCheckLocalDesc())){
            constCheckMap.put("desc","无施工地点");
        }else{
            constCheckMap.put("desc",constCheck.getCheckLocalDesc());
        }
        if(StringUtils.isBlank(constCheck.getUserName())){
            constCheckMap.put("user","无相关记录人");
        }else{
            constCheckMap.put("user",constCheck.getUserName());
        }
        configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
        configuration.setClassForTemplateLoading(this.getClass(), "/templates/pages/domain/pro"); // FTL文件所存在的位置
        Template t=null;
        t = configuration.getTemplate("test.ftl");  // 读取相应的ftl 文件
        Writer out = null;
        response.setContentType("application/vnd.ms-excel");
        String fileName = constCheck.getTaskName() + ".doc";
        fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        response.addHeader("Content-Disposition", "attachment;filename="+ fileName);
        out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream()));
        t.process(constCheckMap, out);
        out.close();


    }



}

private String getImageStr2(String path,String fileName) throws IOException {

    File sf =new File("D:\\temp");
    if(!sf.exists()){
        sf.mkdirs();
    }

    URL url = new URL(path);
    // 打开连接
    URLConnection con = url.openConnection();
    //设置请求超时为5s
    con.setConnectTimeout(5*1000);
    // 输入流
    InputStream is = con.getInputStream();
    // 1K的数据缓冲
    byte[] bs = new byte[1024];
    // 读取到的数据长度
    int len;
    OutputStream os = new FileOutputStream(sf.getPath()+"\\"+fileName);
    // 开始读取
    while ((len = is.read(bs)) != -1) {
        os.write(bs, 0, len);
    }
    // 完毕,关闭所有链接
    os.close();
    is.close();
    String newpath=sf.getPath()+"\\"+fileName;

    // 1、校验是否为空
    if (newpath == null || newpath.trim().length() <= 0) {
        return "";
    }

    // 2、校验文件是否为目录或者是否存在
    File picFile = new File(newpath);
    System.out.println(picFile);
    if (picFile.isDirectory() || (!picFile.exists()))
        return "";

    // 3、校验是否为图片
    try {
        BufferedImage image = ImageIO.read(picFile);
        if (image == null) {
            return "";
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        return "";
    }

    // 4、转换成base64编码
    String imageStr = "   ";
    try {
        byte[] data = null;
        InputStream in = new FileInputStream(newpath);
        data = new byte[in.available()];
        in.read(data);
        BASE64Encoder encoder = new BASE64Encoder();
        imageStr = encoder.encode(data);
    } catch (Exception e) {
        imageStr = "";
        e.printStackTrace();
    }


    delAllFile(newpath);
    return imageStr;
}


public static boolean delAllFile(String path) {
    boolean flag = false;
    File file = new File(path);
    if (!file.exists()) {
        return flag;
    }
    if (!file.isDirectory()) {
        return flag;
    }
    String[] tempList = file.list();
    File temp = null;
    for (int i = 0; i < tempList.length; i++) {
        if (path.endsWith(File.separator)) {
            temp = new File(path + tempList[i]);
        } else {
            temp = new File(path + File.separator + tempList[i]);
        }
        if (temp.isFile()) {
            temp.delete();
        }
        if (temp.isDirectory()) {
            delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
            delFolder(path + "/" + tempList[i]);// 再删除空文件夹
            flag = true;
        }
    }
    return flag;
}


public static void delFolder(String folderPath) {
    try {
        delAllFile(folderPath); // 删除完里面所有内容
        String filePath = folderPath;
        filePath = filePath.toString();
        java.io.File myFilePath = new java.io.File(filePath);
        myFilePath.delete(); // 删除空文件夹
    } catch (Exception e) {
        e.printStackTrace();
    }
}