java 图片导出_java导出含图片的word
prefix="s" uri="/struts-tags"%>
"attachment;filename="+request.getAttribute("strExpFileName")+".doc");%><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
注:此處代碼最好原封不動的,不要給它換行或者調(diào)換代碼位置,因為如果此處甚至一換行都有可能到時候?qū)С鰰rword提示有錯誤,我之前特意試過,至于原因自己也不知道
2.后臺代碼
將將圖片轉(zhuǎn)成base64碼的方法
/**
* 將圖片轉(zhuǎn)成base64碼
* @return
*/
public static String getImageBinary(String imagepath) {
File f = new File(imagepath);
BufferedImage bi;
try {
bi = ImageIO.read(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos);
byte[] bytes = baos.toByteArray();
return encoder.encodeBuffer(bytes).trim();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
調(diào)用該方法
//通過一系列方法通過tomcat獲取你想要圖片的路徑,例如下面是方法之一
ServletActionContext.getServletContext().getRealPath("此處寫上圖片存儲的路徑")
//現(xiàn)在如果已經(jīng)取到了你想要的圖片的路徑saveFilePath。例如
saveFilePath="C:\Program Files\.....\abcd.png";
//將“\”換成“\\”,調(diào)用getImageBinary方法時得是“\\”
saveFilePath=saveFilePath.replace("\\", "\\\\");
StringBuilder str=new StringBuilder();
//取得圖片的base64碼
str.append(getImageBinary(saveFilePath));
images=str.toString();//images就是圖片的base64碼
//此處的strExpFileName就是ExportWord.jsp中上面的變量名
strExpFileName=new String("導出圖片".getBytes("GB2312"), "8859_1");
3.在ExportWord.jsp中剛才去掉base64碼的位置加上
${images}
分享到:
2012-12-07 16:59
瀏覽 14546
評論
1 樓
18335864773
2019-04-02
推薦用 pageoffice 導出 word ,不需要另存 xml 文件,也不需要把圖片轉(zhuǎn)成 base64位。代碼調(diào)用非常簡單。
總結(jié)
以上是生活随笔為你收集整理的java 图片导出_java导出含图片的word的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: python处理windows弹窗_Py
- 下一篇: java list数组排序_浅谈对象数组
