<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
</html>
Java代码有两种方法:
第一是将图片下载到服务端在放到word文档中去,
先建立util类:
public class WordUtil {
private Configuration cfg = null;
public WordUtil() {
cfg = new Configuration();
cfg.setDefaultEncoding("utf-8"); // 设置模板的编码
}
// 模板名 //下载存放的地址
public void createWord(String templetName, String filePathName,
Map<String, Object> dataMap) throws IOException {
cfg.setClassForTemplateLoading(this.getClass(), "/com/word"); // FTL文件所存在的位置
Template temp = null;
try {
// 获取模版文件
temp = cfg.getTemplate(templetName, "utf-8"); // 取出模板的设置编码);
} catch (IOException e) {
e.printStackTrace();
}
// 生成文件的路径和名称
File outFile = new File(filePathName);
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile), "utf-8"));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
temp.process(dataMap, out);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 图片编码
public String getImageStr(String imgFile) {
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
// 文件路径
public String saveFile() {
String nowpath = System.getProperty("user.dir");
String path = nowpath.replace("bin", "webapps");
path += "\\" + "TestWeb" + "\\" + "word";
File tmp = new File(path);
if (!tmp.exists()) {
tmp.mkdirs();
}
return path;
}
}
图片的生成:放到word文档中
String code = request.getParameter("imag");
String[] codes = new String[] { code, };
WordUtil wordUtil = new WordUtil();
String fileStr = wordUtil.saveFile();
try {
for (int i = 0; i < codes.length; i++) {
String[] url = codes[i].split(",");
String u = url[1];
url = null;
// Base64编码
byte[] b = new BASE64Decoder().decodeBuffer(u);
OutputStream out = new FileOutputStream(new File(fileStr+ "\\image" + i + ".png"));
out.write(b);
out.flush();
out.close();
}
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap = getData();
wordUtil.createWord("2.ftl", fileStr + "\\test.doc", dataMap);
} catch (Exception e) {
e.printStackTrace();
}
}
这是生成图片放入服务端,然后在放到word文档中去,
第二种 是直接在word文档中让人图片的编码,个人推荐使用
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String code = request.getParameter("imag"); String [] pngUrlBase64Cds = new String[]{code};
Map<String, Object> dataMap = new HashMap<String, Object>();
for(int i = 0; i < pngUrlBase64Cds.length; i++){
String [] pngBase64Cd = pngUrlBase64Cds[i].split(",");
//图片的64编码在分割后数组的第二个位置
dataMap.put("image"+i, pngBase64Cd[1]);//将图片的64位编码直接放入word文档中去
}
dataMap.put("im0", "图片XX00");//
WordUtil wordUtil = new WordUtil();
String fileStr = wordUtil.saveFile();
try {
wordUtil.createWord("3.ftl", fileStr+"\\test1.doc", dataMap);
} catch (Exception e) {
e.printStackTrace();
}
}
这样就可以了 两种方法,效果是一样的