<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: zhuxianfei
    $Date: 2018-8-27 上午10:38:31
    $Revision: 1.0  
    $Purpose: 扣款文件下载
-->
<a:service xmlns:p="uncertain.proc" xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script"
           trace="true">
    <a:init-procedure>
       <s:server-script><![CDATA[
            importPackage(Packages.java.lang);
            importPackage(Packages.java.io);
            //生成txt的具体路径,环境迁移记得修改
	    var export_path= $bm('ccb.CCB503.ds_abc_interface_export_path').queryAsMap().getChildren();
            var fileDir = export_path[0].attribute_1;
            var document_number = $ctx.parameter.header_number;
            var FilePath = fileDir + $ctx.parameter.header_number + '.txt';

            println(FilePath);
            println(document_number);

            function transfer(file, os) {
                var fis = new FileInputStream(file);
                var b = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024 * 64);
                var len = -1;
                while ((len = fis.read(b)) != -1) {
                    os.write(b, 0, len);
                }
                fis.close();
            }

            function downloadAbcTxt(filePath, fileName) {
                $ctx["__request_type__"] = 'file'; //to indicate this request is not a JSON_REQUEST
                var resp = $ctx['_instance.javax.servlet.http.HttpServletResponse'];
                resp.setHeader("Pragma", "No-cache");
                resp.setHeader("Cache-Control", "no-cache, must-revalidate");
                resp.setDateHeader("Expires", 0);
                resp.setContentType("applicatoin/octet-stream");
                resp.setHeader("Content-disposition", "attachment; filename=" + encodeURI(fileName, 'GBK'));
                var os = resp.getOutputStream();
                transfer(filePath, os);
                os.flush();
            }

            println('start download');
            downloadAbcTxt(FilePath, document_number + '.txt');
            println('end download');

        ]]></s:server-script>
        <p:echo/>
    </a:init-procedure>
</a:service>