zoukankan      html  css  js  c++  java
  • 字符串导出xml文件并弹出下载对话框

    转自:https://blog.csdn.net/zhandingfeng/article/details/53887354

    导出单个xml文件:
    [java] view plain copy
          List<Emr_cda_standard_ntDTO>  resultList = gxwdgfDAO.queryXmlContent(bean,inputData);//字符串来源  
                  
                HttpServletResponse response = (HttpServletResponse) inputData.get(ValueConstant.HTTP_RESPONSE);  
                  
                String xml = resultList.get(0).getContent();    
                  
                //对文件名进行UTF-8编码,并处理编码后空格变成的"+"号  
                String fileName = URLEncoder.encode(resultList.get(0).getCode()+"."+resultList.get(0).getName()+".xml", "UTF-8").replace("+","%20");   
                  
                response.setContentType("application/octet-stream;charset=UTF-8");  
                response.setHeader("Content-Disposition", "attachment;filename="+fileName);  
                  
                PrintWriter out = response.getWriter();  
                if(xml!=null){  
                    out.print(xml);  
                }else{  
                    out.print("");  
                }  
                out.flush();  
                out.close();  
    导出多个xml文件(压缩包形式):

    [java] view plain copy
    HttpServletRequest req = (HttpServletRequest) inputData.get(ValueConstant.HTTP_REQUEST);  
            HttpServletResponse resp = (HttpServletResponse) inputData.get(ValueConstant.HTTP_RESPONSE);  
            String filename = "";  
            String zipName = "共享文档规范.zip";  
            String xml = "";   
            String path = req.getSession().getServletContext().getRealPath("/");  
            try {  
                List<Emr_cda_standard_ntDTO>  resultList = gxwdgfDAO.queryXmlContent(bean,inputData);//字符串来源  
                resp.setContentType("application/octet-stream;charset=UTF-8");  
                resp.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(zipName, "UTF-8"));  
                String str = "";  
                String rt = " ";  
                ZipOutputStream zos = new ZipOutputStream(resp.getOutputStream());  
                for (Emr_cda_standard_ntDTO emrCdaStandardNtDTO : resultList) {  
                    filename = emrCdaStandardNtDTO.getCode()+"."+emrCdaStandardNtDTO.getName()+".xml";  
                    xml = emrCdaStandardNtDTO.getContent();  
                    str += filename + rt;  
                    zos.putNextEntry(new ZipEntry(filename));  
                      
                    //构造一个临时文件用来压缩  
                    File file=new File(path + filename);//文件对象  
                    PrintWriter out=new PrintWriter(new OutputStreamWriter(new FileOutputStream(file),"UTF-8"));//由文件对象构造一个写出器  
                    if(xml!=null){  
                        out.print(xml);  
                    }else{  
                        out.print("");  
                    }  
                    out.flush();  
                    out.close();  
                      
                    FileInputStream fis = new FileInputStream(file);  
                    byte b[] = new byte[1024];  
                    int n = 0;  
                    while((n = fis.read(b)) != -1){  
                        zos.write(b, 0, n);  
                    }  
                    zos.flush();  
                    fis.close();  
                    file.delete();//删除临时文件  
                }  
                zos.setComment("导出成功:" + rt + str);  
                zos.flush();  
                zos.close();  
            } catch (GeneralException ge) {  
                throw ge;  
            } catch (Exception e) {  
                ExceptionUtil.throwUnknown(e, logger);  
            }
    ————————————————
    版权声明:本文为CSDN博主「ZhanBF」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/zhandingfeng/article/details/53887354

  • 相关阅读:
    Delphi通过调用COM对象实现更改桌面壁纸
    Delphi之TDrawGrid绘制
    Delphi中的消息截获(六种方法:Hook,SubClass,Override WndProc,Message Handler,RTTI,Form1.WindowProc:=@myfun)good
    从一般管理原则看微软的重组
    Delphi使用Windows API函数AnimateWindow实现窗体特效
    Delphi下URL汉字编码解码的两个函数
    Delphi2007下CIS的clHttp使用
    Delphi使用XmlHttp获取时间
    提升进程权限为DEBUG权限
    VS 2012 单元测试简单配置
  • 原文地址:https://www.cnblogs.com/sharpest/p/11555795.html
Copyright © 2011-2022 走看看