直接上源码,
@ResponseBody @RequestMapping(value = "/getDocx", method = RequestMethod.GET) public String getDocumentBase64(@RequestBody JSONObject jsonParam, HttpServletResponse res){ String fileName = "default.docx"; String base64String = ""; InputStream in = null; byte[] fileByte = null; try{ JSONObject massageJson = jsonParam.getJSONObject("massage"); // 生成乱码名 String documentTreesJson = jsonParam.getJSONObject("data").toJSONString(); fileName = massageJson.getString("userName") + "__" + massageJson.getString("fileSuffix") + "__" + RandomUtil.getRandomString(32) + ".docx"; // 生成docx 到fileName DocumentUtil.GenerateDocument(documentTreesJson, fileName); in = new FileInputStream(fileName); //通过打开一个到实际文件的连接来创建一个FileInputStream,该文件通过文件系统中的路径名 name 指定。 int size = in.available(); // 返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取(或跳过)的估计剩余字节数。 fileByte = new byte[size]; in.read(fileByte); //从此输入流中将最多 size 个字节的数据读入一个 imageByte字节 数组中。 in.close(); //关闭此文件输入流并释放与此流有关的所有系统资源。 }catch (Docx4JException e){ System.out.println("生成docx文件失败,Json:" + jsonParam.toJSONString()); }catch (Exception e){ System.out.println("创建输出流失败,Json:" + jsonParam.toJSONString()); JSONObject massageJson = jsonParam.getJSONObject("massage"); String documentTreesJson = jsonParam.getString("data"); System.out.println(massageJson.toJSONString()); System.out.println("data: " + documentTreesJson); } finally{ BASE64Encoder encoder = new BASE64Encoder(); base64String = encoder.encode(fileByte);// 对字节数组转换成Base64字符串 if(!IOUtil.delFile(new File(fileName))){ System.err.println("文件未成功删除"); } } // System.out.println(base64String); return base64String; }
热门文章
- 上海领养狗狗机构地址(上海市狗狗领养)
- 11月30日→18.9M/S|2024年最新免费节点V2rayC订阅链接地址
- 1月1日→19.1M/S|2025年最新免费节点V2rayC订阅链接地址分享
- 树的同构 Java
- 11月27日→19M/S|2024年最新免费节点V2rayC订阅链接地址
- 附近宠物领养店电话地址(宠物领养站哪里有)
- 12月14日→20.7M/S|2024年最新免费节点V2rayC订阅链接地址
- 1月27日→18.3M/S|免费节点Clash/SSR/V2rayC订阅链接地址分享
- 1月24日→18.1M/S|免费节点Clash/SSR/V2rayC订阅链接地址分享
- 面试官:@Transactional 注解是如何实现的?面试必问!