空pdf文件生成

InputStream 内容读取

生成空文件

判断操作系统

空pdf文件生成

InputStream 获取

生成空文件
    @RequestMapping("fileFree")
    public String fileFree() throws Exception {

        String winLocationpath = "D:/transfer/";
        String linuxLocationpath = "/user/transfer/";
        String path = "";
        String fileName = "签名文本.txt";
        OsInfo osInfo = SystemUtil.getOsInfo();//获取当前操作系统信息
        if (osInfo.isWindows()) {
            path = winLocationpath;
        }
        if (osInfo.isLinux()) {
            path = winLocationpath;
        }
        File file = new File(path + fileName);

        //判断文本文件是否存在(注意是文本文件)
        if (!FileUtil.exist(file)) {
            FileUtil.touch(file);//创建空文本文件
        }
        FileUtil.appendUtf8String("我是追加的内容", file);
        InputStream inputstream = new FileInputStream(file);
        //获取流信息,可以定位问题,到底有没有获取文件流信息
        System.out.println("文件size:" + String.valueOf(inputstream.available() / 1000) + "k");
        IoUtil.close(inputstream);//注意要关流

        return "";
    }
空pdf文件生成
    @RequestMapping("fileFout")
    public String fileFout() throws Exception {

        String winLocationpath = "D:/transfer/";
        String linuxLocationpath = "/user/transfer/";
        String path = "";
        String fileName = "签名文本.pdf";
        OsInfo osInfo = SystemUtil.getOsInfo();//获取当前操作系统信息
        if (osInfo.isWindows()) {
            path = winLocationpath;
        }
        if (osInfo.isLinux()) {
            path = winLocationpath;
        }
        File file = new File(path + fileName);

        //判断文本文件是否存在(注意是pdf文件)
        if (!FileUtil.exist(file)) {
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(file));//生成pdf模板
            document.open();
            document.add(new Paragraph(" "));//这地方必须要有内容
            document.close();
        }

        InputStream inputstream = new FileInputStream(file);
        System.out.println("文件size:" + String.valueOf(inputstream.available() / 1000) + "k");
        IoUtil.close(inputstream);

        return "";
    }

pdf参考:https://www.jianshu.com/p/9050cb2ae707
pdf参考:https://www.jianshu.com/p/8865433102cf

InputStream 获取
File file = new File(path + fileName);
InputStream inputstream = new FileInputStream(defaultfile);
用到的依赖
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.15</version>
        </dependency>
        
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.10</version>
        </dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值