ins_download.lsc 3.2 KB
<?xml version="1.0" encoding="UTF-8"?>
<a:service xmlns:s="leaf.plugin.script" xmlns:a="http://www.leaf-framework.org/application" xmlns:p="uncertain.proc" trace="true">
    <a:init-procedure>
        <s:server-script><![CDATA[
            //get url to request
            //importPackage(java.util.zip);
            importPackage(java.io);
            importPackage(org.apache.tools.zip); /*可以传入参数*/
            
            function writeFile(zos, fn, fp) {
                var ze = new ZipEntry(fn);
                zos.putNextEntry(ze);
                transfer(fp, zos);
            }
            
            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 getdate() {
                var now = new Date()
                y = now.getFullYear()
                m = now.getMonth() + 1
                d = now.getDate()
                m = m < 10 ? "0" + m : m
                d = d < 10 ? "0" + d : d
                return y + "" + m + "" + d
            }
            //  var url_l = $ctx.parameter.p_url;
            //println(url_l);
            //console.print(url_l);
             var url_l  = 'http://199.10.10.65:8180/reportapp/frameset?__report=reports/policy/policy_others.rptdesign&&__format=PDF';
             $ctx["__request_type__"] = 'file';
            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("application/x-msdownload");
            if (!url_l) {
                //println(record.toXML())//for test
                $logger('alipay.request').info('no url found ');
            } else {
                try {
            
                    var url = new java.net.URL(url_l);
                    var br = new BufferedReader(new InputStreamReader(url.openStream(), 'UTF-8'));
                    var b = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024 * 64);
                    var len = -1;
                    resp.setHeader("Content-disposition", "attachment; filename=121.pdf");
                    var os = resp.getOutputStream();
                    while ((len = br.read(b)) != -1) {
                        println(len);
                        os.write(b, 0, len);
                    }
                    println(len);
                
                    br.close();
                    os.flush();
            
                } catch (e) {
                    //println(e.message)//for test
                    $logger('alipay.request').info('error occured  when request url,' + e.message);
                }
            }
        ]]></s:server-script>
        <!--    <a:model-execute model="inf.INF501.ali_cf_contract_confirm"   /> -->
    </a:init-procedure>
    <a:service-output output="/parameter"/>
</a:service>