废话不多说了 先上代码吧 代码完了之后 在整模板
这是dem的目录结构
好了 作为社会主义接班人 秉承着MVC思想 我写代码的习惯是 先controller 再service
controller 详情 如下
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.transitmonthlyreport.dto.BaseTransferEntity;
import com.transitmonthlyreport.util.MobileConfig;
/**
* 这是一个测试的例子
* @author ZiAn
*
*/
@Controller
@RequestMapping(value = "")
public class TestController {
private static Logger log = Logger.getLogger(TestController.class);
/**
* 注入 测试导出的 TestServiceImpl
*/
@Autowired
private TestServiceImpl testServiceImpl;
/**
* 这是个测试的demo 所以 有些定义的名字比较随意
*
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/service/exportDemo")
@ResponseBody
public BaseTransferEntity export(HttpServletRequest request, HttpServletResponse response) {
BaseTransferEntity baseTransferEntity = new BaseTransferEntity();//这是自己封装的东西
try {
//第一张图片 传图片base64的码
String a = request.getParameter("a");
Map<String, Object> mapImageDown = new HashMap<String, Object>();
mapImageDown.put("a", a);
List<Map<String, Object>> lst = new ArrayList<Map<String,Object>>();
Map<String,Object> newMap = new HashMap<String, Object>();
// 添加个假数据
newMap.put("y", "2020--nian---");
lst.add(newMap);
System.out.println(lst);
// 下载文档到本地
Map<String, Object> maplst = testServiceImpl.downWord(lst, mapImageDown);
// 将上述得到的数据返回给页面 ----自己定义的东西 不重要
baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.success"));
baseTransferEntity.setData(maplst);
baseTransferEntity.setDesc(MobileConfig.get("msg.global.success"));
} catch (Exception e) {
log.error("Test exportDemo--------->" + e.getMessage(), e);
baseTransferEntity.setResultcode(MobileConfig.getStringCode("code.global.error.exception"));
baseTransferEntity.setDesc("系统接口异常,请联系管理员!");
baseTransferEntity.setData(null);
}
return baseTransferEntity;
}
}
接下来 就是serviceImpl了
睁大眼睛哈
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;
/**
* TODO
* <p>
* This contains the following methods:<br/>
* <p>
*
* @author ZiAn
* @version 1.0
* @since 1.0
*/
@Service
public class TestServiceImpl implements TestService {
@Override
public Map<String, Object> downWord(List<Map<String, Object>> lst,Map<String, Object> mapImageDown) throws Exception{
Map<String, Object> retMap = new HashMap<String,Object>();
String a = mapImageDown.get("a").toString();
//下载图片
TestWordUtil wordUtil = new TestWordUtil();
String fileStr = wordUtil.saveFile();
try{
String[] url = a.split(",");
String u = url[1];
// Base64解码
byte[] b1 = new BASE64Decoder().decodeBuffer(u);
// 生成第一张图片
OutputStream out = new FileOutputStream(new File(fileStr+"\\test1.png"));
out.write(b1);
out.flush();
out.close();
//为模板赋值
Map<String, Object> dataMap = new HashMap<String, Object>();
//dataMap =lst.get(0);
dataMap.putAll(lst.get(0));
dataMap.put("a", TestWordUtil.getImageStr(fileStr+"\\test1.png"));
String wordName ="这是文件名称.doc";
//生成word
wordUtil.createWord("TextXML.ftl", fileStr+"\\"+wordName, dataMap);
//返回生成文件路径 以及 文件名称
retMap.put("wordDir", fileStr);
retMap.put("wordName", wordName);
System.out.println("目录="+fileStr+"\\"+wordName);
} catch (IOException e) {
e.printStackTrace();
}
return retMap;
}
}
有了 impl 那肯定得来个接口呀
import java.util.List;
import java.util.Map;
/**
* TODO
* <p>
* This contains the following methods:<br/>
* <p>
*
* @author ZiAn
* @version 1.0
* @since 1.0
*/
public interface TestService {
/**
* 下载文档到本地
* @param lst
* @param mapImageDown
* @return
* @throws Exception
*/
public Map<String, Object> downWord(List<Map<String, Object>> lst, Map<String, Object> mapImageDown) throws Exception;
}
接下来 就是最重要的玩意儿了 util hahaha
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Map;
import sun.misc.BASE64Encoder;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class TestWordUtil {
private Configuration configuration = null;
public TestWordUtil() {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
}
public void createWord(String templetName, String filePathName, Map<String, Object> dataMap) {
configuration.setClassForTemplateLoading(this.getClass(), "/com/transitmonthlyreport/template"); // FTL文件所存在的位置 我这是放在项目底下了 自己的实际路径自己比对
Template t = null;
try {
// 获取模版文件
t = configuration.getTemplate(templetName);
} catch (IOException e) {
e.printStackTrace();
}
// 生成文件的路径和名称
File outFile = new File(filePathName);
Writer out = null;
try {
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile),"UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
t.process(dataMap, out);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static 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 += "\\"+"testWord"+"\\"+"word";
File tmp = new File(path);
System.out.println("path==="+path);
if (!tmp.exists()) {
tmp.mkdirs();
}
return path;
}
public String getEncodeImageStr(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);
}
}
好了 到这个地方 重要的 就来了
制作模板
然后 另存为 xml 格式
都修改好之后 再保存 后缀 修改为 ftl 然后 把这个文件 扔到你的项目里 (就是 模板存放的路径)
然后 运行 tomcat
然后 来自己的路径里看
写这么详细 要是 再看不懂 那我就没办法了