java读取各种文件内容

package com.xahengpin.cqfk.tjfx.common;

import com.xahengpin.common.modules.utils.StringUtils;
import com.xahengpin.xahp.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**

  • @author ssp

  • @className ReadFileConverter

  • @description TODO

  • @create 2021/11/16 10:43

  • 读取文件返回文件内容
    **/
    @RequestMapping(“/readFile”)
    @RestController
    @Api(value = “读取文件”, tags = “读取文件”)
    public class ReadFileConverter {

    /**

    • 上传文件

    • @param file

    • @return
      /
      @PostMapping(value = “/uploadFile”, consumes = "multipart/
      ", headers = “content-type=multipart/form-data”)
      @Inner(value = false)
      public String readFile(@RequestPart(“file”) MultipartFile file) {

      if (file.getOriginalFilename().endsWith(“.txt”) || file.getOriginalFilename().endsWith(“.log”)) {

       return readText(file);
      

      } else if (file.getOriginalFilename().endsWith(“.doc”) || file.getOriginalFilename().endsWith(“.docx”)) {
      return readWord(file);
      } else if (file.getOriginalFilename().endsWith(“.xls”) || file.getOriginalFilename().endsWith(“.xlsx”)) {

       return readExcel(file);
      

      } else {
      return “请选择有效的txt,log,doc,docx,xls,xlsx的文件”;
      }
      }

    /**

    • 读txt.log文件

    • @param file

    • @return
      */
      public static String readText(MultipartFile file) {
      String content = null;
      StringBuffer fsb = new StringBuffer();
      try {
      InputStream stream = file.getInputStream();

       InputStreamReader reader = new InputStreamReader(stream,"GB2312");
       BufferedReader buffReader = new BufferedReader(reader);
       while((content = buffReader.readLine())!=null){
           fsb.append(content);
       }
       buffReader.close();
      

      } catch (Exception e) {
      e.printStackTrace();
      }
      content = fsb.toString();

      return content;
      }

    /**

    • 读word文件
    • @param file
    • @return
      */

    public static String readWord(MultipartFile file) {
    String buffer = “”;
    try {
    if (file.getOriginalFilename().endsWith(“.doc”)) {
    InputStream stream = file.getInputStream();
    WordExtractor ex = new WordExtractor(stream);
    buffer = ex.getText();
    stream.close();
    } else if (file.getOriginalFilename().endsWith(“docx”)) {
    InputStream stream = file.getInputStream();
    XWPFDocument document = new XWPFDocument(stream);
    XWPFWordExtractor xwpfWordExtractor = new XWPFWordExtractor(document);
    buffer = xwpfWordExtractor.getText();
    stream.close();
    } else {
    System.out.println(“此文件不是word文件!”);
    }

     } catch (Exception e) {
         e.printStackTrace();
     }
    
     return buffer;
    

    }

    /**

    • 读取excel文件内容

    • 支持单sheet

    • @param file

    • @return
      */
      public static String readExcel(MultipartFile file) {
      InputStream fileInput = null;//创建文件输入流
      StringBuilder builder = new StringBuilder();
      try {

       fileInput = file.getInputStream();
       if (file.getOriginalFilename().endsWith(".xls")) {
           Workbook wb = new HSSFWorkbook(fileInput);
           Sheet sheetAt = wb.getSheetAt(0);
           //rowBegin代表要开始读取的行号,下面这个循环的作用是读取每一行内容
           for (int i = 0; i <= sheetAt.getLastRowNum(); ++i) {
               Row row = sheetAt.getRow(i);//获取每一行
               int columnNum = row.getLastCellNum();//获取每一行的最后一列的列号,即总列数
               for (int j = 0; j < columnNum; ++j) {
                   Cell cell = row.getCell(j);//获取每个单元格
                   cell.setCellType(Cell.CELL_TYPE_STRING);
                   builder.append(cell.getStringCellValue() + " ");
               }
           }
           System.out.println(builder.toString());
           fileInput.close();
       } else {
           XSSFWorkbook wb = new XSSFWorkbook(fileInput);//由输入流文件得到工作簿对象
           XSSFSheet sheet = wb.getSheetAt(0);//获取第一个sheet
           int lastRowNum = sheet.getLastRowNum(); //获取表格内容的最后一行的行数
           //rowBegin代表要开始读取的行号,下面这个循环的作用是读取每一行内容
           for (int i = 0; i <= lastRowNum; ++i) {
               XSSFRow row = sheet.getRow(i);//获取每一行
               int columnNum = row.getLastCellNum();//获取每一行的最后一列的列号,即总列数
               for (int j = 0; j < columnNum; ++j) {
                   XSSFCell cell = row.getCell(j);//获取每个单元格
                   if (StringUtils.isNotEmpty(cell)){
                       cell.setCellType(Cell.CELL_TYPE_STRING);
                       builder.append(cell.getStringCellValue() + " ");
                   }
      
               }
           }
           System.out.println(builder.toString());
           fileInput.close();
       }
      

      } catch (IOException e) {
      e.printStackTrace();
      }

      return builder.toString();

    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用Java读取OFD文件内容,可以使用第三方库或者开源项目。以下是一个示例: 首先,需要下载并导入一个Java的OFD文件处理库,比如Apache PDFBox或者iText。这些库可以用于处理PDF文件,而OFD文件实际上是一种基于PDF格式的文件。 然后,可以使用以下代码来读取OFD文件内容: ``` import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; public class OFDReader { public static void main(String[] args) { try { // 读取OFD文件 PDDocument document = PDDocument.load(new File("example.ofd")); // 创建PDF文本剥离器 PDFTextStripper pdfStripper = new PDFTextStripper(); // 获取OFD文件的总页数 int pageCount = document.getnumberOfPages(); // 循环读取每一页的内容 for (int i = 1; i <= pageCount; i++) { pdfStripper.setStartPage(i); pdfStripper.setEndPage(i); // 提取该页的文本内容 String pageText = pdfStripper.getText(document); // 输出文本内容 System.out.println("第 " + i + " 页的文本内容:"); System.out.println(pageText); } // 关闭OFD文件 document.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码使用了Apache PDFBox库来加载OFD文件,并使用PDFTextStripper来提取文本内容。可以通过循环遍历每一页,并提取每一页的文本内容。最后,关闭OFD文件。 请注意,OFD文件可能包含多个页面,因此需要在循环中处理每一页的内容。另外,需要根据具体的OFD文件处理库的API文档来使用适当的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

满头黑发到中年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值