zoukankan      html  css  js  c++  java
  • xml或其他附件下载到客户端

    //xml 
                    Document document=DocumentHelper.createDocument();
                    Element root=document.addElement("root");
    
    //。。。。。省略document的过程。。。。。
    
    //下载到指定目录
    OutputFormat format=OutputFormat.createPrettyPrint();
                    format.setEncoding("UTF-8");
                    XMLWriter writer=new XMLWriter(new FileOutputStream(new File("D://dzoa.xml")),format);
                    writer.write(document);
                    writer.close();
    
    //下载到客户端
    DownloadTool.doReady(response, "application/octet-stream", 0, "dzoa.xml");
                    XMLWriter writer=new XMLWriter(response.getOutputStream(), OutputFormat.createPrettyPrint());
                    writer.write(document);
             writer.close();
    
    //相关方法代码
      /**
         * @param mimeType 文件的mime-type,如为null或空字符串,就取multipart/form-data
         * @param filelen  文件长度,大于0表示真实长度,0或负值表示unknown
         * @param fileName 文件名称
         * @param inline   可选项,是inline还是attachment,默认false
         */
        public static void doReady(HttpServletResponse response, String mimeType, long filelen, String fileName, boolean... inline) {
            if (mimeType == null || mimeType.isEmpty()) mimeType = "multipart/form-data";
            fileName = rawEncode(fileName); // RFC 3986
    
            response.setCharacterEncoding("UTF-8");
            response.setContentType(mimeType);
            if (filelen > 0) response.setHeader("Content-Length", String.valueOf(filelen));
    
            // firefox: filename*=utf-8''
            // IE or chrome: filename= or filename*=utf-8''
            // Xunlei: filename= or filename*=utf-8''
            response.setHeader("Content-Disposition", ((inline.length > 0 && inline[0]) ? "inline" : "attachment") + "; filename="" + fileName + """ + "; filename*=utf-8''" + fileName);
        }


    ------------------------------------一般情况的下载附件可调用下列方法-------------------------

       /**
         * @param mimeType 文件的mime-type,如为null或空字符串,就取multipart/form-data
         * @param data     数据
         * @param fileName 文件名称
         * @param inline   可选项,是inline还是attachment,默认false
         */
        public static void download(HttpServletResponse response, String mimeType, byte[] data, String fileName, boolean... inline) {
            response.reset();
            doReady(response, mimeType, (long) data.length, fileName, inline);
    
            OutputStream os = null;
            try {
                os = response.getOutputStream();
                os.write(data);
                os.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                close(os);
            }
        }


    -----------------------------------如果客户端还是没有出来附件下载,检查一下代码---------------------------------------
     //数据导出 
         function gwExport(){
             top.$.fn._confirm("确认要导出数据吗?", function(i){
                 if(1 == i){
                     var cVal = "";
                     var cValStr=[];
                     $("#myTable td input[name=fileCheck]").each(function(){
                         if(this.checked){
                            cValStr.push($(this).val());
                         }
                     });
                     cVal=cValStr.join(",");
                     if(cVal==""){
                         top.$.fn._alert("请选择需要导出的公文。");
                         return;
                     }
    
                     //alert(cVal);
                     location.href = "${ctx}/oa/myflow/procinst/gwExport?" + $.param({"pInsIds": cVal});
                     /* $.post("${ctx}/oa/myflow/procinst/gwExport", {"pInsIds": cVal}, function(data){
                         top.$.fn._alert(data.info);
                         if(data.state==1){
                            // location.reload();
                         }
                     }); */
                 }
             });
         }
    
    
    
    
    
     
     
  • 相关阅读:
    cookie、 sessionStorage 、localStorage之间的区别和使用
    CSS清除浮动大全共8种方法
    需要了解的前端常用js库
    let与const的区别
    java webservice的多种实现方法汇总
    iframe和frame的区别
    Iframe的应用以及父窗口和子窗口的相互访问
    HTML5标签改变
    flex之组件简单应用
    Flex控件之repeater和radioButton应用
  • 原文地址:https://www.cnblogs.com/skyislimit/p/5771830.html
Copyright © 2011-2022 走看看