JAVA批量在html檔案中添加監測代碼。

來源:互聯網
上載者:User

先把所要添加監測內容的html頁面存放到指定目錄.


package struts2;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;public class HandleFiles {public static int count=0;//493public static void getfiles(String path){File file=new File(path);try {count++;String returnStr = "";  InputStreamReader read = new InputStreamReader(new FileInputStream(file),"UTF-8");               BufferedReader reader=new BufferedReader(read);    String line;            while ((line = reader.readLine()) !=null) {                 returnStr+=line+"\n";            }              //returnStr = new String(returnStr.getBytes("GBK"), "UTF-8");            returnStr=returnStr.replace("</head>",getContent());            OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");                  BufferedWriter writer=new BufferedWriter(write);                      writer.write(returnStr);                  writer.close();                 reader.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static String getContent(){String str="";str+= "<script>\n" +" (function(a, e, f, g, b, c, d) {a.ClickiTrackerName = b;\n" +" a[b] = a[b] || function() {(a[b].queue = a[b].queue || []).push(arguments)};\n" +" a[b].start = +new Date; c = e.createElement(f); d = e.getElementsByTagName(f)[0];\n" +" c.async = 1; c.src = g; d.parentNode.insertBefore(c, d)\n" +" })(window, document, 'script', ('https:' == document.location.protocol ? 'https://stm-collect' : 'http://stm-cdn') + '.cn.miaozhen.com/clicki.min.js', 'stm_clicki');\n" +" stm_clicki('create', 'dc-1286', 'auto');\n" +" stm_clicki('send', 'pageview');\n" +"</script>\n";str+="</head>";return str;}  /**     * 讀取某個檔案夾下的所有檔案     */    public static boolean readfile(String filepath) throws FileNotFoundException, IOException {            try {                    File file = new File(filepath);                    if (!file.isDirectory()) {                            System.out.println("檔案");                            System.out.println("path=" + file.getPath());                            getfiles(file.getPath());                    } else if (file.isDirectory()) {                            System.out.println("檔案夾");                            String[] filelist = file.list();                            for (int i = 0; i < filelist.length; i++) {                                    File readfile = new File(filepath + "\\" + filelist[i]);                                    if (!readfile.isDirectory()) {                                            System.out.println("path=" + readfile.getPath());                                            getfiles(readfile.getPath());                                    } else if (readfile.isDirectory()) {                                            readfile(filepath + "\\" + filelist[i]);                                    }                            }                    }            } catch (FileNotFoundException e) {                    System.out.println("readfile()   Exception:" + e.getMessage());            }            return true;    }public static void main(String[] args) {String path="D://bak";try {readfile(path);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("total:"+count);}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.