从网上看到的用例,测试成功
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 "导出成功";