设为首页 加入收藏

TOP

用Aspose-Java免费实现 PDF、Word、Excel、Word互相转换并将转换过得文件上传OSS,返回转换后的文件路径(四)
2023-07-25 21:28:40 】 浏览:77
Tags:Aspose-Java 费实现 PDF Word Excel 相转换 文件上 OSS 后的文 路径
ssUpLoadTools.uploadOssFile(fileName, outputfile); outputfile.delete();
long now = System.currentTimeMillis(); log.info("共耗时:" + ((now - old) / 1000.0) + "秒"); } catch (Exception e) { e.printStackTrace(); } return url; }

四、Word相关操作

1.引入相关依赖

        <dependency>
            <groupId>com.luhuiguo</groupId>
            <artifactId>aspose-words</artifactId>
            <version>23.1</version>
        </dependency>

2.关键代码

    @Override
    public String wordToFile(MultipartFile file, String type) {
        String checkType = FilenameUtils.getExtension(file.getOriginalFilename());
        if (!"doc".equals(checkType) && !"docx".equals(checkType)) {
            throw new ServiceException(1, "输入文件不是Word文件!");
        }
        try {
            switch (type.toUpperCase()) {
                case "TEXT" : {
                    return switchFile(file, SaveFormat.TEXT, "txt");
                }
                case "PDF" : {
                    return switchFile(file, com.aspose.words.SaveFormat.PDF, "pdf");
                }
                /*************** 需要操作每一页Word文件,一般Word类的直接电脑操作,应该用不上************/
//                case "PNG" : {
//                    return switchFile(file, com.aspose.words.SaveFormat.PNG, "png");
//                }
//                case "JPG" : {
//                    return switchFile(file, com.aspose.words.SaveFormat.JPEG, "jpg");
//                }
                default:{}
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    private String switchFile(MultipartFile file, int saveFormat, String suffix){
        String url = "";
        try {
            long old = System.currentTimeMillis();
            // 输出路径
            String fileName = UUID.randomUUID() + "." + suffix;
            String filePath = ossUpLoadTools.getSavePath() + "/" + fileName;
            FileOutputStream os = new FileOutputStream(filePath);
            com.aspose.words.Document doc = new com.aspose.words.Document(file.getInputStream());
            doc.save(os, saveFormat);
            os.close();
            File outputfile  = new File(filePath);
            url = ossUpLoadTools.uploadOssFile(fileName, outputfile);
            outputfile.delete();
            long now = System.currentTimeMillis();
            log.info("共耗时:" + ((now - old) / 1000.0) + "秒");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }

五、PPT相关操作

1.引入相关依赖

 <dependency>
    <groupId>com.luhuiguo</groupId>
<artifactId>aspose-slides</artifactId>
<version>23.1</version>
</dependency>

2.关键部分代码

    @Override
    public String PptToFile(MultipartFile file, String type) {
        // 获取文件后缀名
        String checkType = FilenameUtils.getExtension(file.getOriginalFilename());
        if (!"ppt".equals(checkType) && !"pptx".equals(checkType)) {
            throw new ServiceException(1, "输入文件不是PPT文件!");
        }
        try {
            switch (type.toUpperCase()) {
                case "HTML" : {
                    return SwitchFile(file, com.aspose.slides.SaveFormat.Html, "html");
                }
                case "HTML5" : {
                    return SwitchFile(file, com.aspose.slides.SaveFormat.Html5, "html");
                }
                case "PDF" : {
                    return SwitchFile(file, com.aspose.slides.SaveFormat.Pdf, "pdf");
                }
                default:{}
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    private String SwitchFile(MultipartFile file, int saveFormat, String suffix) {
        String url = "";
        try {
            long old = System.currentTimeMillis();
            String fileName = UUID.ra
首页 上一页 1 2 3 4 5 下一页 尾页 4/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇.MD语法入门,教你写好readme文档 下一篇Spring源码:Bean的生命周期(二)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目