1. 程式人生 > >使用java將html原始碼(拼裝、可獲取頁面原始碼)轉換成為("html頁面",“doc文件檔案”,“pdf格式”),,doc檔案轉換成為pdf,,檔案的刪除、壓縮...

使用java將html原始碼(拼裝、可獲取頁面原始碼)轉換成為("html頁面",“doc文件檔案”,“pdf格式”),,doc檔案轉換成為pdf,,檔案的刪除、壓縮...

目錄:

1、轉換成為html頁面
2、html原始碼轉換成為doc檔案
3、html原始碼轉換成為pdf檔案
4、壓縮多個檔案成為一個zip檔案
5、對檔案進行刪除

實施過程:

A、html頁面的實現:拼裝(獲取)的html程式碼----->html頁面,

B、doc文件檔案的實現:拼裝(獲取)的html程式碼----->doc文件檔案

C、pdf檔案的實現:拼裝(獲取)的html程式碼----->html頁面----->doc文件檔案----->pdf檔案(生成後可自行刪除html、doc檔案)

1、轉換成為html頁面

        /** 
        * 建立html檔案 
        *       name:傳入生成的檔案的檔名

        *       path:存放地址位置

        * 

        *

        *
        * @throws IOException 
        */ 
        public static boolean creatTxtFile(String name) throws IOException { 
            boolean flag = false; 
            filenameTemp = path + name + ".html"; 
            File filename = new File(filenameTemp); 
            if (!filename.exists()) { 
                filename.createNewFile();
                flag = true; 
            }else{
                //倘若檔案存在則將原有檔案移除並建立新的檔案
                filename.delete();
                filename.createNewFile();
                flag = true; 
            }
            return flag; 
        } 
        /** 
        * 將內容寫到html檔案中
        * 
        * @param newStr (html格式的字串)

        *  filenameTemp:檔案路徑

        *
        *            新內容 
        * @throws IOException 
        */
        public static boolean writeHtmlFile (String newStr)throws IOException { 
            // 先讀取原有檔案內容,然後進行寫入操作 
            boolean flag = false; 
            String filein = newStr + "\r\n"; 
            String temp = ""; 
    
            FileInputStream fis = null; 
            InputStreamReader isr = null; 
            BufferedReader br = null; 
    
            FileOutputStream fos = null; 
            PrintWriter pw = null; 
            try { 
                    // 檔案路徑 
                    File file = new File(filenameTemp); 
                    
                    // 將檔案讀入輸入流 
                    fis = new FileInputStream(file); 
                    isr = new InputStreamReader(fis); 
                    br = new BufferedReader(isr); 
                    StringBuffer buf = new StringBuffer(); 
        
                    // 儲存該檔案原有的內容 
                    for (int j = 1; (temp = br.readLine()) != null; j++) { 
                        buf = buf.append(temp); 
                        // System.getProperty("line.separator") 
                        // 行與行之間的分隔符 相當於“\n” 
                        buf = buf.append(System.getProperty("line.separator")); 
                    } 
                    buf.append(filein); 
            
                    fos = new FileOutputStream(file); 
                    pw = new PrintWriter(fos); 
                    pw.write(buf.toString().toCharArray()); 
                    pw.flush(); 
                    flag = true; 
            } catch (IOException e1) { 
                // TODO 自動生成 catch 塊 
                throw e1; }
            finally { 
                if (pw != null) { 
                    pw.close(); 
                }if (fos != null) { 
                    fos.close(); 
                }if (br != null) { 
                    br.close(); 
                }if (isr != null) { 
                    isr.close(); 
                }if (fis != null) { 
                    fis.close(); 
                }} 
                return flag; 
            } 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2、html原始碼轉換成為doc檔案

       /** 
        * HTML檔案轉換成為doc檔案
        *       filePath:檔案儲存的地址

        *       con:html原始碼

        *      FileName:生成的檔名

        *

        *
        * @throws IOException 
        */ 

      

 

//將原始碼寫入Doc文件中,轉換成為Doc文件

 public static boolean writeWordFile(String filePath,String con,String FileName) {
              boolean w = false;
              String path = filePath;
              
              try {
               if (!"".equals(path)) {
                
                // 檢查目錄是否存在
                File fileDir = new File(path);
                if (fileDir.exists()) {
                 
                 // 生成臨時檔名稱              fileName的檔案為生成的檔案為word檔案
                 String fileName =  FileName + ".doc";
                 String content = con;
                 
                 byte b[] = content.getBytes();
                 ByteArrayInputStream bais = new ByteArrayInputStream(b);
                 POIFSFileSystem poifs = new POIFSFileSystem();
                 DirectoryEntry directory = poifs.getRoot();
                 directory.createDocument("WordDocument", bais);
                 FileOutputStream ostream = new FileOutputStream(path+ fileName);
                 poifs.writeFilesystem(ostream);
                 bais.close();
                 ostream.close();
                }
               }
              } catch (IOException e) {
               e.printStackTrace();
              }
              return w;
        }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3、html原始碼轉換成為pdf檔案(轉換成為html頁面不做雷同出來,請參考上文)

       /** 
        * HTML原始碼轉doc檔案轉pdf檔案
        *       pdfFile :生成的pdf檔案路徑

        *       wordFile :doc已存在的檔案路徑

        *      FileName:生成的檔名

        *

        *
        * @throws IOException 
        */ 

    

 

    //封裝doc檔案轉換成為pdf檔案
        public void DocToPdf(String FileName){
            //將doc檔案轉換成為pdf檔案
            String pdfPath =  “D:/"+FileName+".pdf";
            String wordPath =  “D:/"+FileName+".doc";                    
            ActiveXComponent app = null;
            String wordFile = wordPath;
            String pdfFile = pdfPath;
            //System.out.println("開始轉換...");
            // 開始時間
            //long start = System.currentTimeMillis();  
            try {
            // 開啟word
            app = new ActiveXComponent("Word.Application");
            // 設定word不可見,很多部落格下面這裡都寫了這一句話,其實是沒有必要的,因為預設就是不可見的,如果設定可見就是會開啟一個word文件,對於轉化為pdf明顯是沒有必要的
            //app.setProperty("Visible", false);
            // 獲得word中所有開啟的文件
            Dispatch documents = app.getProperty("Documents").toDispatch();
            //System.out.println("開啟檔案: " + wordFile);
            // 開啟文件
            Dispatch document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch();
            // 如果檔案存在的話,不會覆蓋,會直接報錯,所以我們需要判斷檔案是否存在
            File target = new File(pdfFile);  
             if (target.exists()) {  
                target.delete();
             }
            //System.out.println("另存為: " + pdfFile);
            // 另存為,將文件報錯為pdf,其中word儲存為pdf的格式巨集的值是17
            Dispatch.call(document, "SaveAs", pdfFile, 17);
            // 關閉文件
            Dispatch.call(document, "Close", false);
            // 結束時間
            //long end = System.currentTimeMillis();
            //System.out.println("轉換成功,用時:" + (end - start) + "ms");
           }catch(Exception e) {
            System.out.println("轉換失敗"+e.getMessage());
           }finally {
                // 關閉office
            app.invoke("Quit", 0);
           }
        }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4、壓縮多個檔案成為一個zip檔案

        /**
        * 功能:壓縮多個檔案成一個zip檔案
        * @param srcfile:原始檔列表
        * @param zipfile:壓縮後的檔案
        */
        public static void zipFiles(File[] srcfile,File zipfile){
            byte[] buf=new byte[1024];
            try {
              //ZipOutputStream類:完成檔案或資料夾的壓縮
              ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipfile));
              for(int i=0;i<srcfile.length;i++){
                FileInputStream in=new FileInputStream(srcfile[i]);
                out.putNextEntry(new ZipEntry(srcfile[i].getName()));
                int len;
                while((len=in.read(buf))>0){
                  out.write(buf,0,len);
                }
                out.closeEntry();
                in.close();
              }
              out.close();
            } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
          }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5、對檔案進行刪除

 /**
        * 功能:壓縮多個檔案成一個zip檔案
        * @param pathName:原始檔列表
        */        

 

//壓縮完成後刪除壓縮檔案
        public void delFile(String pathName){
             String path = “D:/" + pathName;
             File file = new File(path);
             if(file.isFile()){
                 file.delete();
             }
        }