• 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");
        }
    }
  • 相关阅读:
    1.13 开源软件是什么?有哪些?
    1.10 Linux桌面环境(桌面系统)大比拼[附带优缺点
    Bracket Seuence deletion (找规律,2个元素分别分析一下)
    web学习笔记
    计算机结构 week2
    Alice and the Cake (CF1654C 模拟题,逆续想法)
    star MST (dp)
    For gamers. BY GAMERS (dp预处理+二分)
    Preorder (树贡献问题, 这题关键重复情况的如何判断)
    python 笔记
  • 原文地址:https://www.cnblogs.com/lanliying/p/8821743.html
Copyright © 2020-2023  润新知