• 字符串导出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

  • 相关阅读:
    Civil 3D .NET二次开发第11章代码升级至2018版注意事项
    创建道路曲面
    ObjectARX® for Beginners: An Introduction
    mshcMigrate制作的mshc文件中有链接打不开
    Word 2013无法发布文章到博客园
    ionic 安装插件报错:源文本中存在无法识别的标记
    typescript文件中 使用回调函数无法调用函数外的变量和方法的办法
    ionic2---自定义插件
    angular2----使用swiper做轮播图
    angular2----router
  • 原文地址:https://www.cnblogs.com/sharpest/p/11555795.html
Copyright © 2020-2023  润新知