我有以下情况,我必须修改现有文件并返回包含此修改文件的 zip,我在 Web 应用程序上下文中,我现在所做的是:
///////////////// modifying the existing file with poi librairy
FileInputStream inpoi = new FileInputStream("file_path");
POIFSFileSystem fs = new POIFSFileSystem(inpoi);
HWPFDocument doc = new HWPFDocument(fs);
Range r = doc.getRange();
r.replaceText("<nomPrenom>","test");
byte[] b = doc.getDataStream();
//////////////////////// create the zip file and copy the modified files into it
ZipOutputStream out = new ZipOutputStream(new FileOutputStream("my.zip"));
out.putNextEntry(new ZipEntry("file"));
for (int j = 0; j < b.length; j++) {
out.write(b[j]);
}
鉴于原始文件是用阿拉伯语编写的,因此无法用 word 正确读取创建的压缩文件
我试过这个:
try {
FileInputStream inpoi = new FileInputStream("C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\blame.doc");
POIFSFileSystem fs = new POIFSFileSystem(inpoi);
HWPFDocument doc = new HWPFDocument(fs);
Range r = doc.getRange();
r.replaceText("<nomPrenom>","test");
byte[] stream= doc.getDataStream();
String encoding = "utf-16";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream("yyy.zip"));
ZipEntry zipEntry = new ZipEntry("file.doc");
OutputStreamWriter writer = new OutputStreamWriter(out,"utf-8");
out.putNextEntry(zipEntry);
for (int j = 0; j < stream.length; j++) {
writer.write(stream[j]);
}
writer.close();
} catch (IOException e) {
System.out.println(e.toString());
}
它不起作用