美文网首页
springboot 导入doc文档转成字符串返回

springboot 导入doc文档转成字符串返回

作者: yichen_china | 来源:发表于2021-11-14 20:20 被阅读0次

    依赖1 MultipartFile转 File

    package com.xingchen.common.utils;
    
    import org.springframework.web.multipart.MultipartFile;
    
    public class FilezUtils {
    
        public static File transferToFile(MultipartFile multipartFile) {
    //        选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()方法 。
            File toFile = null;
            if (multipartFile.equals("") || multipartFile.getSize() <= 0) {
              return null;
            } else {
                InputStream ins = null;
                try {
                    ins = multipartFile.getInputStream();
                    toFile = new File(multipartFile.getOriginalFilename());
                    inputStreamToFile(ins, toFile);
                    ins.close();
                }catch (Exception e){
                    return null;
                }
    
            }
            return toFile;
        }
    
        /**
         * 获取流文件
         * @param ins
         * @param file
         */
        private static void inputStreamToFile(InputStream ins, File file) {
            try {
                OutputStream os = new FileOutputStream(file);
                int bytesRead = 0;
                byte[] buffer = new byte[8192];
                while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
                os.close();
                ins.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 删除本地临时文件
         * @param file
         */
        public static void delteTempFile(File file) {
            if (file != null) {
                File del = new File(file.toURI());
                del.delete();
            }
        }
    
    
    }
    
    

    依赖二
    file 转字符串

    package com.xingchen.common.utils;
    
    import org.apache.poi.hwpf.extractor.WordExtractor;
    import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.*;
    import java.io.File;
    import java.io.FileInputStream;
    
    public class WordUtils{
            public static String toString(File filePath) {
                String text = "";
    
                String fileName = filePath.getName().toLowerCase();// 得到名字小写
                try {
                    FileInputStream in = new FileInputStream(filePath);
                    if (fileName.endsWith(".doc")) { // doc为后缀的
    
                        WordExtractor extractor = new WordExtractor(in);
                        text = extractor.getText();
                    }else if (fileName.endsWith(".docx")) { // docx为后缀的
    
                        XWPFWordExtractor docx = new XWPFWordExtractor(new XWPFDocument(in));
                        text = docx.getText();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                return text;
            }
    }
    

    控制器调用

        @PostMapping("/importDoc")
        public AjaxResult importDoc(MultipartFile file, boolean updateSupport) throws Exception {
            LngxPost post =new LngxPost();
            String str = "";
            boolean message = false;
            File filez = null;
            try {
                // 获取文件名
    
                filez= FilezUtils.transferToFile(file);
                str= WordUtils.toString(filez);
                return AjaxResult.success(str);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return AjaxResult.error("文件解析失败");
        }
    

    maven 依赖

            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>4.1.2</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-scratchpad</artifactId>
                <version>4.1.2</version>
                <scope>compile</scope>
            </dependency>
    

    相关文章

      网友评论

          本文标题:springboot 导入doc文档转成字符串返回

          本文链接:https://www.haomeiwen.com/subject/svaualtx.html