问题描述
我有一个 word doc 模板,我必须在其中从 Java 对象动态填充值。文档模板将位于项目类路径中,我必须动态填充这些值并将同一文件保存到共享文件夹位置。
我使用 JCIFS 将文件保存到受限共享位置。文件创建成功,但内容生成不正确。以下是代码,
String user = "username:pwd";
String fileName = outputTemp.docx;
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
System.out.println("Auth =" + auth);
String path = "smb: path" + fileName;
System.out.println("SMB PATH =" + path);
String output = "Downloads\\Projctnam\\src\\main\\resources\\templates\\";
DocxStamper stamper = new DocxStamper(new DocxStamperConfiguration());
InputStream inputStream = new FileInputStream(new File("template.docx"));
OutputStream out = new FileOutputStream(output+fileName);
SmbFile sFile = new SmbFile(path,auth);
SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
sfos.write(out.toString().getBytes());
stamper.stamp(inputStream,variables,out);
sfos.close();
out.close();
代码对我来说似乎没问题。
解决方法
所以,我找到了方法,它现在工作得很好
String user = "username:pwd";
String fileName = outputTemp.docx;
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
System.out.println("Auth =" + auth);
String path = "smb: path" + fileName;
System.out.println("SMB PATH =" + path);
String output = "Downloads\\Projctnam\\src\\main\\resources\\templates\\";
DocxStamper stamper = new DocxStamper(new DocxStamperConfiguration());
InputStream inputStream = new FileInputStream(new File("template.docx"));
//OutputStream out = new FileOutputStream(output+fileName);
SmbFile sFile = new SmbFile(path,auth);
SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
//sfos.write(out.toString().getBytes());
stamper.stamp(inputStream,variables,sfos);
sfos.close();
out.close();