使用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();
             }
        }

猜你喜欢

转载自blog.csdn.net/weixin_42739916/article/details/82493439