我有一个将.docx文件保存到FTP服务器的应用程序。要将文件存储在FTP服务器上,FTP客户端需要文件的InputStream,但问题就出现了。我使用docx4j将我想要的文本保存到文件系统上的临时文件中,之后我获取该文件的InputStream并将其存储在FTP服务器上。但是当我试图打开它时说它已经损坏了。这是我的示例代码:
public void setText(String text) throws BusinessException {
//My utility class, used for ftp processes(such as storing, deleting files
FtpProcesses ftp = new FtpProcesses(Consts.ADMIN_FTP_USERNAME, UserType.ADMIN);
String filePath = ftp.getFileFolderRelativePath(FileType.ARTICLE, String.valueOf(this.getVersionsCount()), String.valueOf(this.getId()));
File outputFile;
InputStream stream;
Text fileText;
R run;
P paragraph;
ObjectFactory factory = Context.getWmlObjectFactory();
try {
outputFile = File.createTempFile("tempFile", ".docx");
paragraph = factory.createP();
run = factory.createR();
fileText = factory.createText();
fileText.setValue(text);
run.getContent().add(fileText);
paragraph.getContent().add(run);
WordprocessingMLPackage word = WordprocessingMLPackage.createPackage();
word.getMainDocumentPart().addObject(paragraph);
word.save(outputFile);
stream = new FileInputStream(outputFile);
ftp.uploadFile(filePath, stream);
} catch (IOException | Docx4JException e) {
e.printStackTrace();
throw new BusinessException(Errors.FILE_UPLOAD_ERROR);
}
}
因此,您可以看到我在保存文件后直接获取InputStream。是否有其他方法可以使用docx4j获取它,以便可以在ftp上正确保存?