java springboot aspose-words word转pdf

《aspose-words官网》
官网下载的是带有水印的,去除水印自己想办法

引入aspose-words-21.1.0-jdk17.jar,这个是第三方包,下载下来idea本地引入

pom.xml引入

		<dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>21.1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/aspose-words-21.1.0-jdk17.jar</systemPath>
        </dependency>

build.gradle引入

    implementation fileTree(dir:'lib',includes:['aspose-words-21.1.0-jdk17.jar'])

文件上传并转换成相应的类型

import com.aspose.words.Document;
import com.aspose.words.SaveFormat;

    private static final String tempPath = System.getProperty("user.dir");//项目根路径
    private static final String FILE_DIR = "/ExchangeFile/";//文件夹
    
	/**
     * 文件类型转换
     * @param file         上传的文件
     * @param exchangeType 需要转换的文件类型
     * @return
     * @throws Exception
     */
    @PostMapping("upload")
    public String upload(@RequestParam("file") MultipartFile file, String exchangeType) throws Exception {
        if (!file.isEmpty()) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            Date date = new Date(System.currentTimeMillis());
            String dateFile = sdf.format(date) + "/";
            File saveDir = new File(tempPath + FILE_DIR + dateFile);
            if (!saveDir.exists()) {
                saveDir.mkdirs();
            }
            //生成的文件名称
            String exchangeFileName = UUID.randomUUID() + "." + exchangeType;
            //生成文件的输出路径
            File exchangeFile = new File(saveDir + "/" + exchangeFileName);
            if (!exchangeFile.exists()) {
                exchangeFile.createNewFile();
            }
            FileOutputStream os = new FileOutputStream(exchangeFile);
            //获取上传的文件流
            Document doc = new Document(file.getInputStream());
            //支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            if ("doc".equals(exchangeType)) {
                doc.save(os, SaveFormat.DOC);
            } else if ("docx".equals(exchangeType)) {
                doc.save(os, SaveFormat.DOCX);
            } else if ("pdf".equals(exchangeType)) {
                doc.save(os, SaveFormat.PDF);
            } else if ("html".equals(exchangeType)) {
                doc.save(os, SaveFormat.HTML);
            } else if ("markdown".equals(exchangeType)) {
                doc.save(os, SaveFormat.MARKDOWN);
            } else if ("png".equals(exchangeType)) {
                doc.save(os, SaveFormat.PNG);
            } else if ("jpeg".equals(exchangeType)) {
                doc.save(os, SaveFormat.JPEG);
            } else if ("gif".equals(exchangeType)) {
                doc.save(os, SaveFormat.GIF);
            } else {
            	//关闭流
                os.close();
                if (exchangeFile.exists()) {
                    exchangeFile.delete();
                }
                //不支持转换的类型
        		//抛出RuntimeException()
                throw new Err(Status.FILE_UNKNOWN_TYPE.getStatus(), Status.FILE_UNKNOWN_TYPE.getError());
            }
            //关闭流
            os.close();
            return dateFile + exchangeFileName;
        } else {
        	//抛出RuntimeException()
            throw new Err(Status.FILE_IS_EMPTY.getStatus(), Status.FILE_IS_EMPTY.getError());
        }
    }

postman测试

在这里插入图片描述

返回的是一个去掉IP:port的uri地址

如:2022/07/19/fac29232-9aaf-4890-810b-07a6313e40bd.pdf
通过IP:port访问
http://localhost:10086/2022/07/19/fac29232-9aaf-4890-810b-07a6313e40bd.pdf
在这里插入图片描述


破解

pom.xml加入依赖

		<!--反编译aspose-words-->
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.27.0-GA</version>
        </dependency>
import javassist.*;

public class DecompileAsposeWords {
    public static void main(String[] args) throws Exception {
        //这一步是完整的jar包路径
        ClassPool.getDefault().insertClassPath("D:/Workspace/demo/src/main/resources/file/aspose-words-21.1.0-jdk17.jar");
        CtClass zzZJJClass = ClassPool.getDefault().getCtClass("com.aspose.words.zzZE0");
        CtMethod zzZ4h = zzZJJClass.getDeclaredMethod("zzZ4h");
        CtMethod zzZ4g = zzZJJClass.getDeclaredMethod("zzZ4g");
        zzZ4h.setBody("{return 1;}");
        zzZ4g.setBody("{return 1;}");
        //反编译后的“zzZE0.class"保存目录[xxx/com/aspose/words/zzZE0.class]
        zzZJJClass.writeFile("D:/Workspace/z");
    }

}

把反编译的文件替换掉官网编译的文件,重新打成jar包,然后本地引用编译后的jar包即可

maven引入本地的jar包
把编译后的jar包放到src同级目录的lib下面,没有lib就创建lib文件夹
在这里插入图片描述

		<dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>21.1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/aspose-words-21.1.0-jdk17-d.jar</systemPath>
        </dependency>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值