标签:style blog http color io os 使用 java ar
(一)功能描述:在智能终端的系统中,对所有学员的信息要生成带有二维码图片的报到证与准考证的word文档
(二)实现过程:
1.创建报到证与准考证的word模板,在模板中填充相应的变量。
List list=this.documentService.printProofByMid();//获取报到证与准考证的信息放到list中 for(int i=0;i<list.size();i++){//通过遍历list,获取list中图片的路径 User u=(User) list.get(i); User uu=new User(); filePath=request.getRealPath("image2/" + u.getBak1() + "_" + u.getName() + ".png");//获取图片的路径 uu.setName(u.getName()); ..........//其他属性同样 uu.setImage(HandlImage.getImageStr(filePath));//调用HandlImage中的getImageStr方法,将图片转为base64码 addList.add(uu);//将对象添加到addList中 } Map<String,Object> map=new HashMap<String,Object>(); if( addList.size()>0){ //对addList进行非空的判断 map.put("TED", addList); //将addList放到map中 DocExportUtils.downLoadDoc(request,map);//调用DocExportUtils下的downLoadDoc方法,生成word文档 }
import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.util.Map; import javax.servlet.http.HttpServletRequest; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class DocExportUtils { private Configuration configuration = null; public DocExportUtils() { configuration = new Configuration(); configuration.setDefaultEncoding("utf-8"); } public static void downLoadDoc(HttpServletRequest request,Map map){ Configuration configuration = new Configuration(); configuration.setDefaultEncoding("utf-8"); configuration.setClassForTemplateLoading(DocExportUtils.class, "/com/ecoinfo/terminal/template");//红色部分存放freemarker模板的路径 Template t=null; try { t = configuration.getTemplate("template.ftl");// //template.ftl为要装载的模板 } catch (IOException e) { e.printStackTrace(); } String name=(String)map.get("title"); //生成word文档的名称 File outFile = new File(request.getRealPath("Word/" + name + ".doc"));// 输出文档路径 Writer out = null; try { out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8")); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { t.process(map, out); } catch (TemplateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
/** * 处理图片 * HandlImage类,用来将图片转为base64码 */ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import com.ecoinfo.commons.security.BASE64Encoder; public class HandlImage{ public static String getImageStr(String filepath){//filepath图片所存放的路径 InputStream in =null; try { in=new FileInputStream(filepath); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if(!"".equals(in)&&in!=null){ byte[]data=null; try{ data=new byte[in.available()]; in.read(data); in.close(); }catch(IOException e){ e.printStackTrace();} BASE64Encoder encoder=new BASE64Encoder(); return encoder.encode(data); }else{ return null; } }
标签:style blog http color io os 使用 java ar
原文地址:http://www.cnblogs.com/spp0152/p/3969926.html