• Java生成静态HTML文件


    private static final String FILEPATH = "/opt/nginx/html/banner/";
        private static  final String RETURNPATH = "/banner/";
    
        private static final String IMAGEPATH="/image_app/scp_image/bannerimg/";
        
        
        private static final String TEMPLATE_PATH = "/opt/nginx/html/banner/modelHtml.html";
        
        
        
        
        @RequestMapping("/submit")
        @ResponseBody
        public Map<String, Object> submit(String imageUrl) {
    
            String str = "";
            StringBuffer buffer = new StringBuffer();
            String uuidFilePath = UUID.randomUUID().toString() +".html";
            try {
                
                /*URL url = new URL(SERVICE_PATH+SAVEHTMLPATH);
                HttpURLConnection httpUrl = (HttpURLConnection)url.openConnection();
                InputStream is = httpUrl.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
                String line;
                while ((line = br.readLine()) != null) {
                    buffer.append(line+"
    ");
                }
                is.close();
                br.close();*/
                
                
                File file = new File(TEMPLATE_PATH);
                if (file.isFile() && file.exists()) {
                    InputStreamReader read = new InputStreamReader(new FileInputStream(file), "utf-8");
                    BufferedReader bufferedReader = new BufferedReader(read);
                    String line = null;
                    while ((line = bufferedReader.readLine()) != null) {
                        System.out.println(line);
                        buffer.append(line+"
    ");
                    }
                    bufferedReader.close();
                }else{
                    System.out.println("system erro");
                }
                
                str = buffer.toString().replaceAll("###imageUrl###", IMAGEPATH+imageUrl);
                
                String saveHtmlFile = FILEPATH + uuidFilePath;
                BufferedWriter bufferedWriter = new BufferedWriter(
                        new OutputStreamWriter(new FileOutputStream(saveHtmlFile), "UTF-8"));
                bufferedWriter.write(str);
                bufferedWriter.newLine();// 换行
                /*
                 * * 刷新该流的缓冲。 关键的一行代码。如果没有加这行代码。数据只是保存在缓冲区中。没有写进文件。 加了这行才能将数据写入目的地。
                 * *
                 */
                bufferedWriter.flush();
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return APIUtil.toMap(HttpStatus.SUCCESS.getValue(), true, RETURNPATH+uuidFilePath);
        }
    复制代码
    复制代码
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    
    public class MakeHtml {
    
        public static boolean JspToHtmlFile(String filePath, String saveHtmlFile) {
                String str = "";
                
                StringBuffer buffer = new StringBuffer();
                
                
                /*try {
                        String tempStr = "";
                        InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "UTF-8");
                        BufferedReader br = new BufferedReader(isr);
                        while ((tempStr = br.readLine()) != null){
                             str = str + tempStr +"
    ";
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                        return false;
                }*/
                
                try {
                    URL url = new URL(filePath);
                    HttpURLConnection httpUrl = (HttpURLConnection)url.openConnection();
                    InputStream is = httpUrl.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
                    StringBuilder sb = new StringBuilder();
                    String line;
                    while ((line = br.readLine()) != null) {
                        buffer.append(line+"
    ");
                    }
                    is.close();
                    br.close();
                } catch (Exception e) {
                    
                }
                
                try {
                        str = buffer.toString().replaceAll("###imageUrl###","http://img.zcool.cn/community/0142135541fe180000019ae9b8cf86.jpg@1280w_1l_2o_100sh.png");
                        //必须设置编码格式不然会出现乱码
                        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(saveHtmlFile),"UTF-8"));
                        bufferedWriter.write(str);
                        bufferedWriter.newLine();//换行
                        bufferedWriter.flush(); 
                        bufferedWriter.close();
                } catch (Exception e) {
                        e.printStackTrace();
                        return false;
                }
                return true;
        }
        
        
        public static void main(String[] args) {
            long begin = System.currentTimeMillis();
            String url = "http://xxx/banner/modelHtml.html";// 模板文件地址
            String savepath = "d:/" + begin + ".html";// 生成文件地址
            JspToHtmlFile(url, savepath);
            System.out.println("用时:" + (System.currentTimeMillis() - begin) + "ms");
        }
    }
    复制代码
  • 相关阅读:
    算法之--回溯法-迷宫问题【python实现】
    awk积累
    mysql自动化安装脚本(二进制安装)
    ${FUNCNAME[@]}和$LINENO使用
    shell脚本配置ssh免密登陆
    /etc/passwd和/etc/group文件详解
    Bagging与随机森林算法原理小结
    js之如何获取css样式
    Jetty源码学习-编译Jetty源码二三事
    maven安装和与IDE集成
  • 原文地址:https://www.cnblogs.com/zhuyeshen/p/11435593.html
Copyright © 2020-2023  润新知