<?xml version="1.0" encoding="UTF-8"?>
<a:screen xmlns:p="uncertain.proc" xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script" trace="true">
    <a:init-procedure>
        <p:echo/>
        <s:server-script><![CDATA[
            importPackage(java.io);

            var request = $ctx['_instance.javax.servlet.http.HttpServletRequest'];
            var response = $ctx['_instance.javax.servlet.http.HttpServletResponse'];
            try{
            data = $bm('hls.HLS536.excel_system_parameter').queryAsMap().getChildren();
            var base_dir=data[0].base_journal_dir;
            var year=data[0].year;
            var month=data[0].month;
            var factory = new Packages.org.apache.commons.fileupload.disk.DiskFileItemFactory();
            factory.setSizeThreshold(10);
            var up = new Packages.org.apache.commons.fileupload.servlet.ServletFileUpload(factory);
            var items = up.parseRequest(request);
            var i = items.iterator();
            while (i.hasNext())  {
            var item = i.next();
            if (item.isFormField()) {
            println("item.getFieldName()"+item.getFieldName() + "=" + item.getString());
            } else {
            var file_old_name =item.getName();
            //println('文件名'+file_old_name);
            var file_type_name = item.getName().substr(item.getName().lastIndexOf("."));
            var file_type = item.getName().substr(item.getName().lastIndexOf(".")+1);
            var guid_file_name_path = $bm('cont.CON500.con_contract_get_guid_file_name').queryAsMap();
            var guid_file_name_tables = guid_file_name_path.getChildren();
            var file_name=guid_file_name_tables[0].guid_file_name;
             var file_path = base_dir+year+"\\"+month+"\\";
             var dir=new File(String(file_path));
             if(!dir.exists()){
                createDir(file_path);
                  }
            var f = item.getStoreLocation();
            var input,output;
            var tempFile=new File(String(f));
            try{

            input = new FileInputStream(tempFile);
            output = new FileOutputStream(file_path+file_name);
            var buf = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024);
            var bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
            output.write(buf, 0, bytesRead);
            }
            input.close();
            output.close();
             }catch(e){
            //删除临时表
            $bm('hls.HLS536.hls_attachment_tmp').delete();
            println('上传文件出错,请联系管联系管理员理员！e'+e);
            if(input!=null){
            input.close();
            }
            if(output!=null){
            output.close();
            }
            raise_app_error('上传文件出错,请联系管理员！'+String(e.javaException.getMessage()));
            println('上传文件出错,请联系管理员！e');
            }
           var file_length=get_file_length(file_path+file_name);
            //文件上传成功后进行附件挂载
            $bm('hls.HLS536.insert_hls_journal_tmp').update({
                "file_path":String(file_path+file_name),
                 "file_name":file_old_name,
                "file_type":file_type,
                file_length:file_length
                           })
           //删除临时表
           $bm('hls.HLS536.hls_attachment_tmp').delete();
            }
            }
            }catch (e) {
            //删除临时表
            $bm('hls.HLS536.hls_attachment_tmp').delete();
            raise_app_error(e);

            }



            function createDir(dir){
            var file=new File(String(dir));
            if(!file.exists()){//如果文件夹不存在
            file.mkdirs();//创建文件夹
            }

            }

            //获取文件长度
            function get_file_length(path){
            	var file=new File(String(path));
            	if (file.exists() && file.isFile()){
            		//println(f.length());
            		length=file.length();
            		return length;
                } else{
                	return 0;
                }
            }




        ]]></s:server-script>
    </a:init-procedure>
    <a:view>
    <a:link id="hls_journal_entrance_import_link" url="${/request/@context_path}/modules/hls/HLS536/hls_journal_import_entrance.lview"/>
         <script type="text/javascript"> <![CDATA[
         window.location.href = $('hls_journal_entrance_import_link').getUrl();

        ]]></script>
    </a:view>
</a:screen>
