asposeDOC转PDF java文件转换

实现将用户上传的file转为pdf存附件服务

aspose官方文档  ,用的破解jar包,具体用法可参考官方文档

public String doc2pdf(File file,String fileName,String fileUrl) throws Exception{
        FileOutputStream os = null;
        FileInputStream is = null;
        try {
            //验证License 若不验证则转化出的pdf文档会有水印产生
            if (!getLicense()) {
                return null;
            }
            long old = System.currentTimeMillis();
            //临时文件,可以放在项目目录下,用完删除,但一定要有文件夹,没有的话需要创建

            File changedFile = new File("e://text.pdf");
            os = new FileOutputStream(changedFile);
            is = new FileInputStream(file);
            Document document = new Document(is);
            //支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF...
            //保存具有新名称和文件格式的文档
            document.save(os, SaveFormat.PDF);
            //这时候上传changedFile到自家附件服务就好...
            long now = System.currentTimeMillis();
            logger.error("doc文件转为pdf文件共耗时:" + ((now - old) / 1000.0) + "秒");
            return ossUrl;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(os != null){
                try {
                    os.close();
                   //关闭输出流删除临时文件
                } catch (IOException e) {
      
                    e.printStackTrace();
                }
            }

            if(is != null){
                try {
                   //关闭输入流
                    is.close();
               
                } catch (IOException e) {
      
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    private boolean getLicense() throws Exception{
        boolean result = false;
        try {
           //license.xml文件放在resources目录下
            InputStream is=
                xxx.class.getClassLoader().getResourceAsStream("license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

java中File类是操作文件和目录的,流分为输入流和输出流,输入流是用来写的,输出流是用来读的

一个说的比较清楚的栗子:(来自:https://blog.csdn.net/jiangxinyu/article/details/7885518#

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class testFile {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// file(内存)----输入流---->【程序】----输出流---->file(内存)
		File file = new File("d:/temp", "addfile.txt");
		try {
			file.createNewFile(); // 创建文件
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		// 向文件写入内容(输出流)
		String str = "亲爱的小南瓜!";
		byte bt[] = new byte[1024];
		bt = str.getBytes();
		try {
			FileOutputStream in = new FileOutputStream(file);
			try {
				in.write(bt, 0, bt.length);
				in.close();
				// boolean success=true;
				// System.out.println("写入文件成功");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			// 读取文件内容 (输入流)
			FileInputStream out = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(out);
			int ch = 0;
			while ((ch = isr.read()) != -1) {
				System.out.print((char) ch);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值