智能仲裁后端服务

ReadFileUtils.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package com.ruoyi.common.utils;
  2. import org.apache.poi.hwpf.extractor.WordExtractor;
  3. import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
  4. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  5. import java.io.*;
  6. /**
  7. * @author wangqiong
  8. * @description 读取文件内容
  9. * @date 2023-12-11 11:45
  10. */
  11. public class ReadFileUtils {
  12. public static String readerTxtFile(String filePath){
  13. BufferedReader br=null;
  14. StringBuilder result=new StringBuilder();
  15. try {
  16. br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)),"GBK"));
  17. String line=null;
  18. while ((line=br.readLine())!=null) {
  19. result.append(line).append("\n");
  20. }
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. } finally {
  24. if (null!=br){
  25. try {
  26. br.close();
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  32. return result.toString();
  33. }
  34. public static String readWord(String filePath) throws Exception{
  35. File file = new File(filePath);
  36. if(file.length()==0) return ""; // 需要操作原因是可能会空文件问题,如果不做处理,在下面读取中会报错
  37. StringBuffer sb = new StringBuffer();
  38. String buffer = "";
  39. try {
  40. if (filePath.endsWith(".doc")) {
  41. InputStream is = new FileInputStream(file);
  42. WordExtractor ex = new WordExtractor(is);
  43. buffer = ex.getText();
  44. if(buffer.length() > 0){
  45. //使用回车换行符分割字符串
  46. String [] arry = buffer.split("r\\n");
  47. for (String string : arry) {
  48. sb.append(string.trim());
  49. }
  50. }
  51. } else if (filePath.endsWith(".docx")) {
  52. FileInputStream fis = new FileInputStream(file);
  53. XWPFDocument xdoc = new XWPFDocument(fis);
  54. XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
  55. buffer = extractor.getText();
  56. sb.append(buffer!=null?buffer:"");
  57. // OPCPackage opcPackage = POIXMLDocument.openPackage(filePath);
  58. // XWPFWordExtractor extractor = new XWPFWordExtractor(opcPackage);
  59. // buffer = extractor.getText();
  60. // if(buffer.length() > 0){
  61. // //使用换行符分割字符串
  62. // String [] arry = buffer.split("\n");
  63. // for (String string : arry) {
  64. // sb.append(string.trim());
  65. // }
  66. // }
  67. } else {
  68. return null;
  69. }
  70. return sb.toString();
  71. } catch (Exception e) {
  72. System.out.print("error---->"+filePath);
  73. e.printStackTrace();
  74. return null;
  75. }
  76. }
  77. }