Linux环境 使用easypoi操作word 报空指针,在windwos下正常
XWPFDocument doc = WordExportUtil.exportWord07(convertTemplatePath(templatePath), params);
使用工具类对路径进行处理
public static String convertTemplatePath(String path) {
// 如果是windows 则直接返回
if (System.getProperties().getProperty("os.name").contains("Windows")) {
return path;
}
Resource resource = new ClassPathResource(path);
FileOutputStream fileOutputStream = null;
// 将模版文件写入到 tomcat临时目录
String folder = System.getProperty("catalina.home");
File tempFile = new File(folder + File.separator + path);
// System.out.println("文件路径:" + tempFile.getPath());
// 文件存在时 不再写入
if (tempFile.exists()) {
return tempFile.getPath();
}
File parentFile = tempFile.getParentFile();
// 判断父文件夹是否存在
if (!parentFile.exists()) {
parentFile.mkdirs();
}
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(resource.getInputStream());
fileOutputStream = new FileOutputStream(tempFile);
byte[] buffer = new byte[10240];
int len = 0;
while ((len = bufferedInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return tempFile.getPath();
}