日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POI导出word带图片及本地测试没问题,在服务器上找不到模板的问题

發布時間:2023/12/31 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POI导出word带图片及本地测试没问题,在服务器上找不到模板的问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

https://gitee.com/hffs/poi-tl
官網:http://deepoove.com/poi-tl/
依賴
poi-tl需要poi版本高于4.1.1,但是poi版本4.1.1沒有XWPFTemplate,所以這里用的poi版本是4.1.2
poi-tl版本1.0.0 不能new PictureRenderData獲取圖片,所以這里用的1.8.2

<!-- excel工具 --><poi.version>4.1.2</poi.version><poi-tl.version>1.8.2</poi-tl.version><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>${poi.version}</version></dependency><!-- world工具 --><dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>${poi-tl.version}</version></dependency>

FileUtil

public class FileUtil { /*** 下載文件到瀏覽器* @param request* @param response* @param filename 要下載的文件名* @param file 需要下載的文件對象* @throws IOException*/public static void downFile(HttpServletRequest request, HttpServletResponse response, String filename, File file) throws IOException {// 文件存在才下載if (file.exists()) {OutputStream out = null;FileInputStream in = null;try {// 1.讀取要下載的內容in = new FileInputStream(file);// 2. 告訴瀏覽器下載的方式以及一些設置// 解決文件名亂碼問題,獲取瀏覽器類型,轉換對應文件名編碼格式,IE要求文件名必須是utf-8, firefo要求是iso-8859-1編碼String agent = request.getHeader("user-agent");if (agent.contains("FireFox")) {filename = new String(filename.getBytes("UTF-8"), "iso-8859-1");} else {filename = URLEncoder.encode(filename, "UTF-8");}// 設置下載文件的mineType,告訴瀏覽器下載文件類型String mineType = request.getServletContext().getMimeType(filename);response.setContentType(mineType);// 設置一個響應頭,無論是否被瀏覽器解析,都下載response.setHeader("Content-disposition", "attachment; filename=" + filename);// 將要下載的文件內容通過輸出流寫到瀏覽器out = response.getOutputStream();int len = 0;byte[] buffer = new byte[1024];while ((len = in.read(buffer)) > 0) {out.write(buffer, 0, len);}} catch (IOException e) {e.printStackTrace();} finally {if (out != null) {out.close();}if (in != null) {in.close();}}}} /*** 遞歸刪除目錄下的所有文件及子目錄下所有文件** @param filePath 將要刪除的文件目錄路徑* @return boolean Returns "true" if all deletions were successful.* If a deletion fails, the method stops attempting to* delete and returns "false".*/public static boolean deleteDir(String filePath) {File dir = new File(filePath);if (dir.isDirectory()) {String[] children = dir.list();//遞歸刪除目錄中的子目錄下for (int i = 0; i < children.length; i++) {boolean success = deleteDir(filePath + File.separator + children[i]);if (!success) {return false;}}}// 目錄此時為空,可以刪除return dir.delete();} }

測試


圖片格式(最上面官網中)如下:

Map<String, Object> data = new HashMap<String, Object>();// 安檢人員data.put("execUser", "值");// 安檢結果data.put("status", "值");// gasStoveUrl 為圖片的地址data.put("gasStoveUrl", new PictureRenderData(125, 125, ".png", BytePictureUtils.getUrlBufferedImage(gasStoveUrl))) // 其他屬性代碼都省略 // 寫入word輸出try {//模板位置ClassPathResource template = new ClassPathResource("模板文件.docx");String filePath = template.getFile().getPath();XWPFTemplate xwpfTemplate = XWPFTemplate.compile(filePath).render(data);//文件名String docName = DateUtil.DateToString(new Date(), DateStyle.YYYYMMDDHHMMSS) + ".docx"; File targetFile = new File(docName);FileOutputStream out = new FileOutputStream(targetFile);xwpfTemplate.write(out);out.flush();out.close();xwpfTemplate.close();// 下載輸出到瀏覽器FileUtil.downFile(request,response,docName,targetFile);FileUtil.deleteDir(targetFile.getPath());} catch (Exception e) {log.info("文件生成失敗:"+e.getMessage());throw new DataNotFoundException("文件生成失敗:"+e.getMessage());}

以上在本地沒問題,打成jar包在服務器上就找不到模板
本來以為是路徑的問題,但是測試后發現絕對路徑和相對路徑都訪問不到模板
原來是使用ClassPathResource獲取classpath下文件失敗了
返回的是一個Jar協議地址:jar:file:/xxx/xx.jar!/xxxx。
參考自:https://www.renfei.net/posts/1003293
解決方案如下圖
昨邊為之前的只能在本地的
右邊為更改后

InputStream is = getClass().getClassLoader().getResourceAsStream(url);XWPFTemplate xwpfTemplate = XWPFTemplate.compile(is).render(data);

總結

以上是生活随笔為你收集整理的POI导出word带图片及本地测试没问题,在服务器上找不到模板的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。