生成xml字符串并下载(前台通过按…

情景:
      前台通过传递两个参数semId、semNum,后台通过这两个参数进行数据库查询,返回相关课件信息,将这些课件信息组装成xml文件格式的字符串。然后将字符串转换成输入流。然后再response走流写入到前台。前台回弹出一个提示框,提示你下载。


前台触发该下载的是一个js事件:
  function downLoadCourseOfSem(){
                      //下面使用的easyui的控件,其是就是判断一下是否有对象选中,因为我是根据这个对象的semId和semNum去查询后台数据的
                  var node = $('#admintable').datagrid('getSelected');
                  if (node == null) {
                            alert("请选择要下载课件信息的批次对象");
                            return;
                    }
                 
                // 实现文件下载保存。此是通过点击按钮触发xml下载事件。后台处理完成后,它会自动弹出一个下载提示框。当然这跟你后台response的设置时有关的。这样就可以实现 异步查询数据并下载xml格式文件的功能了。window.location.href="url";其实这个解析成html 与 <a href ="url"></a>是一样效果的。

                  window.location.href='${ctx}/health/healthTopboxSemester_downLoadCourseOfSem?'+'semId=' + node.semId +'&semNum='+node.semNum;

            }

后台处理方法:

 
      public String downLoadCourseOfSem() throws Exception{
            THealthTopbox tHealthTopbox=null;
            String orgId="";
           
            ActionContext context = ActionContext.getContext();
            HttpServletResponse response = (HttpServletResponse) context.get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
            response.setCharacterEncoding("UTF-8");

              //蓝色部分为将查询出来的数据组装成xml格式的字符串
            StringBuffer downBuffer=new StringBuffer();
        downBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
        downBuffer.append("<downData>");
        
            HttpServletRequest request = (HttpServletRequest) context.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
            String fileUrl=request.getRequestURL().toString();
            fileUrl=fileUrl.split("public")[0];
            request=null;
            
            List<Map<String, Object>> tmpList=this.semCourseservice.findCourseBySemId(semId);//查询该培训批次下的所有课件信息
            
            if(tmpList!=null && tmpList.size()>=0){
                
                downBuffer.append("    <cwCount>"+tmpList.size()+"</cwCount>");
                int counrSize=0;
                //                                          4
                //tcw.cw_Id,tcw.cw_Name,tcw.file_Name,tcw.file_Path,tcw.file_Size
                String oneCwId="";
                String oneCwName="";
                String oneFileName="";
                String oneFilePath="";
                String fileName="";
                String cwOrder="";
                int oneSize=0;
                for(Map<String,Object> objArr:tmpList){
                    oneCwId=String.valueOf(objArr.get("semNum"))+""+String.valueOf(objArr.get("cwId"));
                    oneCwName=String.valueOf(objArr.get("cwName").toString());
                    
                    cwOrder=String.valueOf(objArr.get("cwOrder").toString());
                    fileName=String.valueOf(objArr.get("fileName").toString());
                    oneFileName=cwOrder+fileName;
                    oneFilePath=String.valueOf(objArr.get("filePath").toString());
                    oneSize=IntegerUtil.parseInteger(String.valueOf(objArr.get("fileSize")));
                    counrSize+=oneSize;
                    downBuffer.append("    <courseware>");
                    downBuffer.append("        <cwId>"+oneCwId+"</cwId>");
                    downBuffer.append("        <cwName>"+oneCwName+"</cwName>");
                    downBuffer.append("        <fileName>"+oneFileName+"</fileName>");
                    downBuffer.append("        <fileSize>"+oneSize+"M</fileSize>");
                    downBuffer.append("        <downUrl>"+oneFilePath+"</downUrl>");
                    downBuffer.append("</courseware>");
                }
                String showSize="";
                DecimalFormat df = new DecimalFormat("#");
                if(counrSize>1024){
                    showSize=DoubleUtil.roundNumber((DoubleUtil.parseDouble(counrSize+"")/1024), 2)+"G";
                }else{
                    showSize=counrSize+"M";
                }
                downBuffer.append("    <countSize>"+showSize+"</countSize>");
            }
        
        downBuffer.append("</downData>");
        logger.info(downBuffer.toString());

           
            InputStream stream = new ByteArrayInputStream(downBuffer.toString().getBytes("UTF-8")); // 将字符串转换成输入流
            OutputStream os = null;
            response.setContentType("APPLICATION/DOWNLOAD");
            response.addHeader("Content-Disposition","attachment; filename="+new String( ("培训批次编号"+semNum+"下所含的的课件信息.xml").getBytes("gb2312"), "ISO8859-1" )); // 这里是为了防止文件名称中文乱码
            response.setContentLength(stream.available());
            os=response.getOutputStream();
           
            int iBytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((iBytesRead = stream.read(buffer, 0, 8192)) != -1) {
            os.write(buffer, 0, iBytesRead);
            }
            os.close();
            response.flushBuffer();
           
            return null;
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值