java导出word文档,客户端打开

分为两个部分:

1.生成word文档(略去生成模板文件过程)

2.发送至客户端

    private boolean createDoc(Map<String, String> map, String filePath,
            String fileName) {
        boolean createFlag = false;
        Template temp = null;
        File docFile = null;
        Writer docout = null;
        try {

            Configuration cfg = new Configuration();
            cfg.setDefaultEncoding("utf-8");
            cfg.setDirectoryForTemplateLoading(new File(filePath));
            cfg.setObjectWrapper(new DefaultObjectWrapper());


            temp = cfg.getTemplate("123.ftl");
            temp.setEncoding("utf-8");

            docFile = new File(filePath + "/" + fileName);
            docout = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(docFile)));
            temp.process(map, docout);
            createFlag = true;/
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (TemplateException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (docout != null) {
                try {
                    docout.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        return createFlag;
    }

    private void deleteFile(String filePath, String fileName) {
        Log.info("delete start~~~~~~~~~~~~~");
        File docFile = null;
        docFile = new File(filePath + "/" + fileName);
        if (docFile.exists() && docFile.isFile()) {
            docFile.delete();
        }
        Log.info("delete over~~~~~~~~~~~~~");
    }

    private void downloadFile(String filePath, String fileName,
            HttpServletResponse response) {
        Log.info("download start~~~~~~~~~~~~~");
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        OutputStream fos = null;
        InputStream fis = null;
        String fileShowName = "test.doc";
            response.setContentType("application/msword");
            response.setHeader("Content-disposition", "inline;filename="
                    + fileShowName);

            fis = new FileInputStream(filePath + "/" + fileName);
            bis = new BufferedInputStream(fis);
            fos = response.getOutputStream();
            bos = new BufferedOutputStream(fos);

            int bytesRead = 0;
            byte[] buffer = new byte[100 * 1024]; 

            while ((bytesRead = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                if (bis != null) {
                    bis.close();
                }
                if (fos != null) {
                    fos.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

发送至客户端打开方式在上述代码的response.setHeader()中设置。


转载于:https://my.oschina.net/zhaoxiaohuan/blog/191250

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值