满地殇,花落 2022-05-18 19:36 采纳率: 50%
浏览 27
已结题

Java 图片写入.doc 遇到头疼问题

从网上看到的用例,测试成功

  public static void main(String[] args) {

        try {
            // 创建word文档,并设置纸张的大小
            Document doc = new Document(PageSize.A4);
            //当前用户桌面路径
            File desktopDir = FileSystemView.getFileSystemView() .getHomeDirectory();
            String desktopPath = desktopDir.getAbsolutePath();
            System.out.println("desktopPath = " + desktopPath);
            LocalDate date = LocalDate.now();
            String s = desktopPath+ "\\"+date;
            /**
             * 建立一个书写器与document对象关联,通过书写器可以将文档写入到输出流中
             */
            //OutputStream os = new FileOutputStream(new File(s+".doc"));
            RtfWriter2.getInstance(doc, new FileOutputStream(s+".doc"));
            doc.open();
            // 在列中添加图片
            Image png = Image
                    .getInstance("d:\\22.png");
            //Image png1 = Image.getInstance("E:\\File\\YIH02-63-M.JPG");
            doc.add(png);
            //doc.add(png1);
            //Paragraph p = new Paragraph("Hello World ");
            //doc.add(p);
            doc.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

但是开发中,前端传来,将图片转base64,我转成multipartFile1 ,下一步就不知道怎么做了,那位仁兄有案例的,发我一下,帮忙解答一下 感谢

 public String downloadDoc(@RequestBody(required = false)String multipartFile, String projectName, HttpServletResponse response) {
        try {
            MultipartFile multipartFile1 = BASE64DecodedMultipartFile.base64ToMultipart(multipartFile);
            InputStream inputStream = multipartFile1.getInputStream();

            //DOMBase64Transform.getInstance()
            // 创建word文档,并设置纸张的大小
            Document doc = new Document(PageSize.A4);
            //当前用户桌面路径
            File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
            String desktopPath = desktopDir.getAbsolutePath();
            LocalDate date = LocalDate.now();
            String s = desktopPath+ "\\"+projectName+date;

            //建立一个书写器与document对象关联,通过书写器可以将文档写入到输出流中
            RtfWriter2.getInstance(doc, new FileOutputStream(s + ".doc"));
            doc.open();

            Image png = Image.getInstance(inputStream);
            doc.add(inputStream);
            doc.close();
            response.setContentType("application/msword;charset=UTF-8");
           // response.setHeader("Content-Disposition", "attachment; filename=" + new String(title.getBytes("utf-8"), "ISO8859-1") + ".doc");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "导出成功";

img

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 5月26日
    • 创建了问题 5月18日

    悬赏问题

    • ¥15 sql server 2012的下载出错
    • ¥15 图像识别用户软件开发
    • ¥20 类原生rom lineageos
    • ¥15 有没有会做中专,云计算,卷子的,有偿一百块
    • ¥15 HC32串口DMA循环发送数据
    • ¥15 Uni-App实现飞书授权登陆
    • ¥50 Qt应用中如何通过代码打开开发者工具devtools
    • ¥20 mpp硬解码h264转为yuv
    • ¥40 怎样批量对比两个数据库的表差异
    • ¥15 lettuce连接哨兵redis,主从切换异常