当前位置:   article > 正文

java实现爬虫技术,读取txt,word,excel,ppt,pdf,html等格式的文件_java如何爬取网站上的pdf

java如何爬取网站上的pdf

最近跟我同事一起做的项目要求读取txt,word,excel,ppt,pdf,html中的内容,不多说,先把代码贴出来,之后有时间再来做详细的解读。

这是读取txt文件

[html]  view plain  copy
  1. /**  
  2.      * 获取txt的文件内容 新建的默认格式 ,其它三种格式会乱码  
  3.      *   
  4.      * @param txtFile  
  5.      * @return  
  6.      */  
  7.     public String GetTxtContent(File txtFile) {  
  8.         BufferedReader reader = null;  
  9.           
  10.         String tempString = null;  
  11.         StringBuffer contents = new StringBuffer();  
  12.         try {  
  13.             reader = new BufferedReader(new FileReader(txtFile));  
  14.             while ((tempString = reader.readLine()) != null) {  
  15.                 contents.append(tempString);  
  16.             }  
  17.             reader.close();  
  18.         } catch (FileNotFoundException e) {  
  19.             e.printStackTrace();  
  20.         } catch (IOException e) {  
  21.             e.printStackTrace();  
  22.         } finally {  
  23.             if (reader != null) {  
  24.                 try {  
  25.                     reader.close();  
  26.                 } catch (IOException e) {  
  27.                     e.printStackTrace();  
  28.                 }  
  29.             }  
  30.         }  
  31.         return contents.toString().trim();  
  32.     }  
[html]  view plain  copy
  1. <h1>读取ppt</h1>  /**  
  2.      * 读取PPT的内容  
  3.      *   
  4.      * @param excleFile  
  5.      * @return  
  6.      */  
  7.     public String GetPPTContent(File excleFile) {   
  8.         StringBuffer contents = new StringBuffer("");// 文档内容  
  9.         InputStream is = null;  
  10.         SlideShow ppt = null;  
  11.         try {  
  12.             is = new FileInputStream(excleFile);  
  13.             ppt = new SlideShow(new HSLFSlideShow(is));  
  14.         } catch (FileNotFoundException e1) {  
  15.             e1.printStackTrace();  
  16.         } catch (IOException e1) {  
  17.             e1.printStackTrace();  
  18.         }  
  19.         Slide[] slides = ppt.getSlides();  
  20.   
  21.         for (int i = 0; i < slides.length; i++) {  
  22.             TextRun[] t = slides[i].getTextRuns();// 为了取得幻灯片的文字内容,建立TextRun  
  23.             for (int j = 0; j < t.length; j++) {  
  24.                 contents.append(t[j].getText());// 这里会将文字内容加到content中去  
  25.             }  
  26.         }  
  27.         if (is != null) {  
  28.             try {  
  29.                 is.close();  
  30.             } catch (IOException e) {  
  31.                 e.printStackTrace();  
  32.             }  
  33.         }  
  34.         return contents.toString().trim();  
  35.     }  
[html]  view plain  copy
  1. <h1>读取excel</h1>    /**  
  2.      * 获取2007excle的内容  
  3.      *   
  4.      * @param exclexlsxFile  
  5.      * @return  
  6.      */  
  7.     public String GetExclexlsxContent(File exclexlsxFile) {  
  8.         StringBuffer content = null;  
  9.         XSSFWorkbook workbook = null;  
  10.         InputStream in = null;  
  11.         try {  
  12.             in = new FileInputStream(exclexlsxFile);  
  13.             content = new StringBuffer();  
  14.             workbook = new XSSFWorkbook(in);  
  15.         } catch (FileNotFoundException e) {  
  16.             e.printStackTrace();  
  17.         } catch (IOException e) {  
  18.             e.printStackTrace();  
  19.         }  
  20.   
  21.         for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {  
  22.             XSSFSheet aSheet = workbook.getSheetAt(numSheets);// 获得一个sheet  
  23.             content.append("\n");  
  24.             if (null == aSheet) {  
  25.                 continue;  
  26.             }  
  27.             for (int rowNum = 0; rowNum <= aSheet.getLastRowNum(); rowNum++) {  
  28.                 content.append("\n");  
  29.                 XSSFRow aRow = aSheet.getRow(rowNum);  
  30.                 if (null == aRow) {  
  31.                     continue;  
  32.                 }  
  33.   
  34.                 for (short cellNum = 0; cellNum <= aRow.getLastCellNum(); cellNum++) {  
  35.                     XSSFCell aCell = aRow.getCell(cellNum);  
  36.                     if (null == aCell) {  
  37.                         continue;  
  38.                     }  
  39.                     if (aCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {  
  40.                         content.append(aCell.getRichStringCellValue()  
  41.                                 .getString());  
  42.                     } else if (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {  
  43.                         boolean b = HSSFDateUtil.isCellDateFormatted(aCell);  
  44.                         if (b) {  
  45.                             Date date = aCell.getDateCellValue();  
  46.                             SimpleDateFormat df = new SimpleDateFormat(  
  47.                                     "yyyy-MM-dd HH:mm:ss");  
  48.                             content.append(df.format(date));  
  49.                         }  
  50.                     }  
  51.                 }  
  52.             }  
  53.         }  
  54.         if (in != null) {  
  55.             try {  
  56.                 in.close();  
  57.             } catch (IOException e) {  
  58.                 e.printStackTrace();  
  59.             }  
  60.         }  
  61.   
  62.         return content.toString().trim();  
  63.     }  
  64.     /**  
  65.      * 读取excle的内容  
  66.      *   
  67.      * @param excleFile  
  68.      * @return  
  69.      */  
  70.     public String GetExcleContent(File excleFile) {  
  71.         StringBuffer content = null;  
  72.         HSSFWorkbook workbook = null;  
  73.         InputStream in = null;  
  74.         try {  
  75.             in = new FileInputStream(excleFile);  
  76.             content = new StringBuffer();  
  77.             workbook = new HSSFWorkbook(in);  
  78.         } catch (FileNotFoundException e) {  
  79.             e.printStackTrace();  
  80.         } catch (IOException e) {  
  81.             e.printStackTrace();  
  82.         }  
  83.   
  84.         for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {  
  85.             HSSFSheet aSheet = workbook.getSheetAt(numSheets);// 获得一个sheet  
  86.             content.append("\n");  
  87.             if (null == aSheet) {  
  88.                 continue;  
  89.             }  
  90.             for (int rowNum = 0; rowNum <= aSheet.getLastRowNum(); rowNum++) {  
  91.                 content.append("\n");  
  92.                 HSSFRow aRow = aSheet.getRow(rowNum);  
  93.                 if (null == aRow) {  
  94.                     continue;  
  95.                 }  
  96.   
  97.                 for (int cellNum = 0; cellNum <= aRow.getLastCellNum(); cellNum++) {  
  98.                     HSSFCell aCell = aRow.getCell(cellNum);  
  99.                     if (null == aCell) {  
  100.                         continue;  
  101.                     }  
  102.   
  103.                     if (aCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {  
  104.                         content.append(aCell.getRichStringCellValue()  
  105.                                 .getString());  
  106.                     } else if (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {  
  107.                         boolean b = HSSFDateUtil.isCellDateFormatted(aCell);  
  108.                         if (b) {  
  109.                             Date date = aCell.getDateCellValue();  
  110.                             SimpleDateFormat df = new SimpleDateFormat(  
  111.                                     "yyyy-MM-dd HH:mm:ss");  
  112.                             content.append(df.format(date));  
  113.                         }  
  114.                     }  
  115.                 }  
  116.             }  
  117.         }  
  118.         if (in != null) {  
  119.             try {  
  120.                 in.close();  
  121.             } catch (IOException e) {  
  122.                 e.printStackTrace();  
  123.             }  
  124.         }  
  125.   
  126.         return content.toString().trim();  
  127.     }  
[html]  view plain  copy
  1. <span style="font-size:48px;">读取word</span>  
  2.     /**  
  3.      * 获取word的内容  
  4.      *   
  5.      * @param wordPath  
  6.      *            文件  
  7.      * @return word的内容  
  8.      */  
  9.     @SuppressWarnings("resource")  
  10.     public  String GetWordContent(File wordFile) {  
  11.         String strContent = "";  
  12.         FileInputStream in=null;  
  13.         try {  
  14.             in = new FileInputStream(wordFile);  
  15.             WordExtractor text = new WordExtractor(in);  
  16.             strContent = text.getText();  
  17.         } catch (Exception e) {  
  18.             e.printStackTrace();  
  19.         }finally{  
  20.             if(in!=null){  
  21.                 try {  
  22.                     in.close();  
  23.                 } catch (IOException e) {  
  24.                     // TODO Auto-generated catch block  
  25.                     e.printStackTrace();  
  26.                 }  
  27.             }  
  28.         }  
  29.   
  30.         return strContent.trim();  
  31.     }  
  32.     /**  
  33.      * 获取word2007的内容  
  34.      *   
  35.      * @param word2007Path  
  36.      * @return  
  37.      * @throws Exception  
  38.      */  
  39.     public String GetWordDocxContent(File wordDocxFile) {  
  40.         POIXMLTextExtractor extractor;  
  41.         String text2007 = "";  
  42.         try {  
  43.             OPCPackage opcPackage = POIXMLDocument.openPackage(wordDocxFile  
  44.                     .getPath());  
  45.             extractor = new XWPFWordExtractor(opcPackage);  
  46.             text2007 = extractor.getText();  
  47.         } catch (IOException e) {  
  48.             e.printStackTrace();  
  49.         } catch (XmlException e) {  
  50.             e.printStackTrace();  
  51.         } catch (OpenXML4JException e) {  
  52.             e.printStackTrace();  
  53.         }  
  54.         return text2007.trim();  
  55.     }  
[html]  view plain  copy
  1. <span style="font-size:48px;">读取pdf</span>  
[html]  view plain  copy
  1. /**  
  2.  * 读取PDF文字的内容  
  3.  *   
  4.  * @param pdfPath  
  5.  *            pdf  
  6.  * @return 返回pdf文件的内容  
  7.  */  
  8. public String GetPDFContent(File pdfFile) {  
  9.     String content = "";  
  10.     FileInputStream is = null;  
  11.     PDDocument doc = null;  
  12.     try {  
  13.         is = new FileInputStream(pdfFile);  
  14.         PDFParser parser = new PDFParser(is);  
  15.         parser.parse();  
  16.         doc = parser.getPDDocument();  
  17.         PDFTextStripper stripper = new PDFTextStripper();  
  18.         content = stripper.getText(doc);  
  19.     } catch (Exception e) {  
  20.         e.printStackTrace();  
  21.     } finally {  
  22.         if (is != null) {  
  23.             try {  
  24.                 is.close();  
  25.             } catch (Exception e) {  
  26.                 e.printStackTrace();  
  27.             }  
  28.         }  
  29.         if (doc != null) {  
  30.             try {  
  31.                 doc.close();  
  32.             } catch (Exception e) {  
  33.                 e.printStackTrace();  
  34.             }  
  35.         }  
  36.     }  
  37.     return content.trim();  
  38. }  
[html]  view plain  copy
  1. <span style="font-size:48px;">读取html</span>  
  2.     /**  
  3.      * 读取网页纯文本内容用来存储索引方法*/  
  4.     public  String GetHTML(String url) throws ParserException{  
  5.         Parser parser = new Parser(url);  
  6.         StringBean sb=new StringBean();  
  7.         //設置不需要頁面的鏈接信息  
  8.         sb.setLinks(false);  
  9.         //設置將不間斷空格由正規空格替代  
  10.         sb.setReplaceNonBreakingSpaces(true);  
  11.         //設置一系列空格由單一空格代替  
  12.         sb.setCollapse(true);  
  13.         parser.visitAllNodesWith(sb);  
  14.         return sb.getStrings().trim();  
  15.     }  
  16.     /**@param filePath   
  17.      * 文件上傳路徑  
  18.      * 处理附件方法 获得JSON数组  
  19.      * @throws Exception */  
  20.     public String HandleFj(String param,IService service,String filePath) throws Exception{  
  21.         JSONArray json=null;  
  22.         ArrayList<IEntity>list=null;  
  23.         String sql="";  
  24.         String fjtotalpath="";  
  25.         try {  
  26.             json=JSONArray.fromObject(DataObject.getObjectValue("param"));  
  27.         } catch (Exception e) {  
  28.             e.printStackTrace();  
  29.             return "";  
  30.         }  
  31.         if(!StringHelper.isNullOrEmpty(json)){  
  32.             StringBuffer fjcontenttotal=new StringBuffer();  
  33.             for(int i=0;i<json.length();i++){  
  34.                 String fileid=json.getJSONObject(i).getString("id");//拿到fileid  
  35.                 String name=json.getJSONObject(i).getString("name");  
  36.                 if(!StringHelper.isNullOrEmpty(fileid)&&!StringHelper.isNullOrEmpty(name)){  
  37.                     sql="select t.localpath from t_srffile t where t.file_id='"+fileid+"'";  
  38.                     try {  
  39.                         list=service.selectRaw(sql, null);  
  40.                     } catch (Exception e) {  
  41.                         e.printStackTrace();  
  42.                     }  
  43.                     for(IEntity o:list){  
  44.                         String location=DataObject.getStringValue(o.get("location"));  
  45.                         fjtotalpath=filePath+location;  
  46.                         fjcontenttotal.append(this.GetFileContent(fjtotalpath));  
  47.                     }  
  48.                 }  
  49.                 return fjcontenttotal.toString();  
  50.             }  
  51.         }  
  52.         return "";  
  53.     }  
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/743035
推荐阅读
相关标签
  

闽ICP备14008679号