多叉树遍历生成文件提供下载
2011-08-30 13:23
323 查看
树节点的结构和构造函数
exportcasefile 的实现:
download下载函数实现:
前台一句话实现下载:
package com.domain; import java.io.Serializable; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import com.help.XmlFunc; import com.service.TestCaseService; import com.service.TestGroupService; public class GroupNode implements Serializable { private static final long serialVersionUID = 1L; private int parentId;//parentgroupid private int selfId;//groupid private String nodeName;//groupname private GroupNode gn;// private List<TestGroup> childList;//childgrouplist private List<TestCase> tclist;//caselist //构造函数 public GroupNode(int groupid){ TestGroupService tgs = new TestGroupService(); TestCaseService tcs = new TestCaseService(); this.selfId = groupid; this.parentId = tgs.getParentGroupid(groupid); //this.gn = tgs.getGroupInfo(groupid); this.nodeName = tgs.getGroupInfo(groupid).getGroupname(); this.childList = tgs.getChildGroupList(groupid); this.tclist = tcs.getCaseList(groupid); } /** * 按层递归遍历,每次将node下面的tclist到处到文件 */ public void traverse(String projectname,String dir) throws ParserConfigurationException{ //将group下面的用例列表导出到文件 XmlFunc.exportcasefile(this.tclist,this.nodeName,this.selfId,projectname,dir); if(this.selfId==0){ return ; } if(childList==null || childList.isEmpty()){ return ; } int childNumber=childList.size(); for(int i= 0 ; i < childNumber;i++) { TestGroup child=childList.get(i); int childgroupid = child.getGroupid(); GroupNode gn = new GroupNode(childgroupid); gn.traverse(projectname,dir); } } public boolean isLeaf() { if(childList==null){ return true; } else{ if(childList.isEmpty()){ return true; }else{ return false; } } } public int getParentId() { return parentId; } public int getSelfId() { return selfId; } public String getNodeName() { return nodeName; } public List<TestGroup> getChildList() { return childList; } public List<TestCase> getTclist() { return tclist; } }
exportcasefile 的实现:
public static int exportcasefile(List<TestCase> tclist,String groupname,int groupid,String projectname,String dir) throws ParserConfigurationException { //创建xml对象,将从数据库中读出的数据写到xml中 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); factory.setIgnoringElementContentWhitespace(true); DocumentBuilder db=factory.newDocumentBuilder(); Document xmldoc=db.newDocument(); Element testgroup = xmldoc.createElement("testgroup"); TestGroupService tgs = new TestGroupService(); String prefix = tgs.getAbsolutelyGroupname(groupid); testgroup.setAttribute("module", prefix+groupname); testgroup.setAttribute("projectname",projectname); for(int i = 0;i<tclist.size();i++){ Element testcase = xmldoc.createElement("testcase"); TestCase tc = tclist.get(i); String casecode = tc.getTestcasename(); String description = tc.getDescription(); String classname = tc.getClassname(); String caselevel = tc.getCaselevel(); int caseid = tc.getCaseid(); //String creator = tc.getCreator(); testcase.setAttribute("casecode", casecode); testcase.setAttribute("description", description); testcase.setAttribute("classname", classname); testcase.setAttribute("caselevel", caselevel); TestStepService tss = new TestStepService(); List<TestStep> tslist = tss.getStepList(caseid); for(int j = 0;j<tslist.size();j++){ Element teststep = xmldoc.createElement("step"); TestStep ts = tslist.get(j); String type = ts.getSteptype(); String method = ts.getStepmethod(); String stepdescription = ts.getDescription(); String stepmodule = ts.getModule(); String stepclassname = ts.getClassname(); int stepid = ts.getId(); teststep.setAttribute("type", type); teststep.setAttribute("method", method); teststep.setAttribute("description", stepdescription); if((stepmodule !=null)&& (stepmodule !="")){ teststep.setAttribute("module", stepmodule); } if((stepclassname !=null)&& (stepclassname !="")){ teststep.setAttribute("classname", stepclassname); } TestDataService tds = new TestDataService(); List<TestData> tdlist = tds.getTestDataList(stepid); for(int k = 0;k<tdlist.size();k++){ Element testdata = xmldoc.createElement("inputdata"); TestData td = tdlist.get(k); String key = td.getK_key(); String value = td.getValue(); String datatype = td.getType(); String datadescription = td.getDescription(); testdata.setAttribute("key", key); testdata.setAttribute("value", value); testdata.setAttribute("description", datadescription); testdata.setAttribute("datatype", datatype); teststep.appendChild(testdata); } testcase.appendChild(teststep); } testgroup.appendChild(testcase); } //将写完的xml对象输出到文件 saveXml(dir+"//"+groupname+".xml",testgroup); return 0; }savexml实现:
public static void saveXml(String fileName, Node node) {//将Document输出到文件 TransformerFactory transFactory=TransformerFactory.newInstance(); try { Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); DOMSource source=new DOMSource(); source.setNode(node); StreamResult result=new StreamResult(); result.setOutputStream(new FileOutputStream(fileName)); transformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }下载文件的servlet的代码:
GroupNode gn = new GroupNode(groupid); try{ //生成时间戳 long timestamp = System.currentTimeMillis(); timestamp = timestamp/1000; String dir = tempFile+timestamp; //如果不存在,则创建这个目录 File f=new File(dir); if(!f.exists()) f.mkdirs(); gn.traverse(projectname,dir);
//对导出的文件进行压缩 CompressFile bean = new CompressFile(); String zipfilename = zipFile+timestamp+".zip"; try{ bean.zip(dir, zipfilename);
//删除导出的临时文件 DeleteFileUtil.delete(dir); response = download(zipfilename,response);
//删除生成的zip压缩文件 DeleteFileUtil.delete(zipfilename); }catch(Exception e){ e.printStackTrace(); } }catch(Exception e){ e.printStackTrace(); }
download下载函数实现:
public HttpServletResponse download(String path, HttpServletResponse response) { try { // path是指欲下载的文件的路径。 File file = new File(path); // 取得文件名。 String filename = file.getName(); // 取得文件的后缀名。 String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase(); // 以流的形式下载文件。 InputStream fis = new BufferedInputStream(new FileInputStream(path)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); // 设置response的Header response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes())); response.addHeader("Content-Length", "" + file.length()); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (IOException ex) { ex.printStackTrace(); } return response; }
前台一句话实现下载:
function exportcasefile(groupid){ window.open('DownloadCasefile?groupid='+groupid,'#'); }
相关文章推荐
- Struts2 遍历输出WEB-INF文件目录,并提供下载功能
- 在服务器端生成Excel文件后如何提供客户端下载 Response.Redirect()
- [Python]cherrypy环境下生成csv文件,提供下载
- php生成csv文件提供给用户下载
- 动态生成.txt文件并提供下载
- DataTable导出生成Excel文件,并提供下载!
- asp.net动态生成txt文件并提供用户下载代码
- 打包成apk,生成apk文件,上传到网站服务器提供链接下载
- 把生成的excel文件直接提供为下载页效果
- php生成csv文件并提供下载及相关注意事项
- 前端数据传到后台动态生成Excel文件并提供文件下载
- 提供下载:批量图片压缩(生成缩略图或指定大小图片)及文件批量改名器
- 【提供源码下载】使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用MFC提供的Http类下载和上传文件
- ASP.NET提供文件下载函数(支持大文件、续传、速度限制、资源占用小)
- C#生成CHM文件(应用篇)之代码库编辑器(4)【附程序最终版下载】
- 【转】PHP读取服务器端文件提供弹出下载窗口
- 华为code craft软件精英赛笔记-如何在Linux下利用提供的shell文件生成tar.gz赛题提交压缩包
- php 提供文件下载的2中方式