转自: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 = "\r\n";  
            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

posted on 2019-09-20 11:13  Sharpest  阅读(537)  评论(0编辑  收藏  举报