package com.cirle.scientific.utils;/*
 *24901
 *2023/1/4
 *14:00
 */



import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URL;

/**
 * @author 24901
 */
@Slf4j
public class ReadWordUtil {


    //使用路径读取word文件
    public static String readWord(String path){
        String buffer = "";
        try {
            if (path.endsWith(".doc")){
                InputStream is = new FileInputStream(new File(path));
                WordExtractor ex = new WordExtractor(is);
                buffer = ex.getText();
            }else if (path.endsWith(".docx")){
                FileInputStream fs = new FileInputStream(new File(path));
                XWPFDocument xdoc = new XWPFDocument(fs);
                XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
                buffer = extractor.getText();
            }
        }catch (Exception e){
            log.info("类:ReadWordUtil 更具文件路径提取word文件内容报错 e->{}", e);
            e.printStackTrace();
        }
        return buffer;
    }

    //使用MultipartFile 接收文件流
    public static void importWord(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        String fileName = file.getOriginalFilename();
        String suff = fileName.substring(fileName.lastIndexOf(".") + 1);
        String content = "";
        if (suff.equals("docx")) {
            XWPFDocument xdoc = new XWPFDocument(inputStream);
            XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
            content = extractor.getText();
            System.out.println(content);
            extractor.close();
        } else if (suff.equals("doc")) {
            WordExtractor ex = new WordExtractor(inputStream);
            content = ex.getText();
            System.out.println(content);
            ex.close();
        } else {
            System.out.println("此文件不是word文件");
        }
    }

    public static File getNetUrlHttp(String path) throws IOException {
        File file = null;
        if(path.endsWith(".doc")){
            file  = File.createTempFile("wordText",".doc");
        }else if(path.endsWith(".docx")){
            file  = File.createTempFile("wordText",".docx");
        }
        if(file != null){
            FileUtils.touch(file);
        }
        URL urlfile;
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try{
            //判断文件的父级目录是否存在，不存在则创建
            assert file != null;
            mkdirs(file.getParent());
            //下载
            urlfile = new URL(path);
            inputStream = urlfile.openStream();
            outputStream = new FileOutputStream(file);

            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead=inputStream.read(buffer,0,8192))!=-1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }catch (Exception e) {
            e.printStackTrace();
            log.info("类:ReadWordUtil url地址转文件方法报错 e->{}", e);
        }finally {
            try {
                if (null != outputStream) {
                    outputStream.close();
                }
                if (null != inputStream) {
                    inputStream.close();
                }

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

    /**
     * 1.5 当文件夹不存在时，mkdirs会自动创建多层目录，区别于mkdir。 (mkdir如果父目录不存在则会抛出异常)
     * @param destPath  存放目录
     */
    public static void mkdirs(String destPath) {
        File file = new File(destPath);
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
    }

    public static void main(String[] args) throws IOException {
        File file = null;
        file = ReadWordUtil.getNetUrlHttp("url");
        String test = ReadWordUtil.readWord(file.getPath());
    }
}
