ERP通过JAVA流的形式将数据传到外围系统
1、ERP封装数据成XML写入数据库服务器指定文件
--指定相关文件信息
v_file_path := '/u01/test/app/fs1/EBSapps/appl/cux/12.0.0/forms'; vfilepath := 'TO_SSNC_PATH9'; vfilename := 'voucher.xml'; v_sender := '001';
--封装数据 vclobpz_result := '<?xml version=''' || '1.0' || ''' encoding=''' || 'UTF-8' || '''?><ufinterface account=''' || 'SS' || ''' billtype=''' || 'vouchergl' || ''' businessunitcode=''' || 'develop' || ''' filename=''' || 'voucher.xml' || ''' groupcode=''' || 'SS' || ''' orgcode=''' || l_orgcode || ''' receiver=''' || '0001A5100000000005F3' || ''' sender=''' || '001' || '''><voucher><voucher_head><pk_voucher></pk_voucher><pk_vouchertype>01</pk_vouchertype><year>' || l_year || '</year><pk_system>GL</pk_system><voucherkind>0</voucherkind><pk_accountingbook>' || l_orgcode || '-0001</pk_accountingbook><discardflag>N</discardflag><period>' || l_period || '</period><no></no><attachment>1</attachment><prepareddate>' || to_char(last_day(SYSDATE) - 6, 'YYYY-MM-DD') || '</prepareddate><pk_prepared>' || l_prepared || '</pk_prepared><pk_casher></pk_casher><signflag>Y</signflag><pk_checked></pk_checked><tallydate></tallydate><pk_manager></pk_manager><memo1></memo1><memo2></memo2><reserve1></reserve1><reserve2>N</reserve2><siscardflag /><pk_org>' || l_orgcode || '</pk_org><pk_org_v>' || l_orgcode || '</pk_org_v><pk_group>SS</pk_group><details>'; --写入文件 x_msg_data := NULL; BEGIN v_create_file := 'A'; --打开文件 R读文本,W写文本 ,A附加文本 vmyfile := utl_file.fopen(vfilepath, vfilename, 'W', 30000); v_create_file := 'B'; --写入xml到指定文件 utl_file.put_line(vmyfile, convert(vclobpz_result, 'ZHS16GBK')); --需要转换格式,否则可能会出现乱码 v_create_file := 'C'; --关闭文本 utl_file.fclose(vmyfile); EXCEPTION WHEN OTHERS THEN IF (v_create_file = 'A') THEN x_msg_data := '打开XML文件异常:' || SQLERRM; ELSIF (v_create_file = 'B') THEN x_msg_data := '写入数据到XML文件异常:' || SQLERRM; ELSIF (v_create_file = 'C') THEN x_msg_data := '关闭XML文件异常:' || SQLERRM; END IF; END;
传入外围系统,获取返回结果 SELECT cux_nc_ap_voucher_pkg.cux_java_sendfile(v_addr, v_file_path, vfilename) INTO v_return FROM dual;
2、读取文件,通过java流传给外围系统
create or replace and compile java source named APPS.cux_java_sendfile as import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.traversal.NodeIterator; import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.*; public class cux_java_sendfile { public static String xzSendFile(String addr,String filepath,String filename)throws IOException { System.out.println("Test1"); File vfile= new File(filepath+File.separator+filename); String conn_flag1=null; cux_java_sendfile sc = new cux_java_sendfile(); //验证是否已连接NC System.out.println("Test2"); String conn_flag= sc.getConn_flag(addr); if (conn_flag!="获取连接出错!") { System.out.println(filepath+File.separator+filename); if (vfile.exists()){ //sc.sendMessage(vfile,false,sc.getConn(false,addr)); try{ conn_flag1= sc.receiveResponse(vfile,sc.getConn(addr)); } catch (Exception e) { } }else{ return "文件不存在"; } } return conn_flag1; } private boolean ret = false; String message = ""; String fileMessage = ""; //String returnmsg =""; /** * 取得NC外部平台连接 * * @param bcompress * 是否启用压缩 * @return * @throws IOException */ public HttpURLConnection getConn(String url) throws IOException { // 连接NC外部平台地址 try { URL realURL = new URL(url); HttpURLConnection connection = (HttpURLConnection) realURL .openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-type", "text/xml"); connection.setRequestMethod("POST"); System.out.println("取得连接" + url); return connection; } catch (MalformedURLException e) { System.out.println("获取连接出错!"); return null; } } public static String getConn_flag(String url) throws IOException { // 连接NC外部平台地址 try { URL realURL = new URL(url); HttpURLConnection connection = (HttpURLConnection) realURL .openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-type", "text/xml"); return "取得连接" + url; } catch (MalformedURLException e) { System.out.println("获取连接出错!"); //return NULL; return "获取连接出错!"; } } public String receiveResponse(File file,HttpURLConnection connection//,String post,String post1 ) throws Exception { try{ String post = ""; System.out.println("==================BEGIN===================="); // 获取URLConnection对象对应的输出流 connection.setRequestProperty("Charset", "UTF-8"); //设置编码为utf-8 PrintWriter printWriter = new PrintWriter(connection.getOutputStream()); // 发送请求参数 BufferedInputStream input = new BufferedInputStream( new FileInputStream(file)); int length; int bufflength = 40960 * 1; byte[] buffer = new byte[bufflength]; while ((length = input.read(buffer)) != -1) { post = new String(buffer,0,length); printWriter.write(post);//post的参数 xx=xx&yy=yy //System.out.println(post); } // flush输出流的缓冲 printWriter.flush(); //开始获取数据 BufferedInputStream bis = new BufferedInputStream(connection.getInputStream()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len; byte[] arr = new byte[1024]; while((len=bis.read(arr))!= -1){ bos.write(arr,0,len); bos.flush(); } bos.close(); System.out.println("=======bos======="+bos.toString("UTF-8")); //解析NC返回结果集的XML System.out.println("解析NC返回结果集的XML"); Document doc = null; String return_msg = ""; String return_Suc = ""; //从xml文档中获取DOM的解析器... DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //这里调用DOM工厂... try { //从DOM工厂中获取DOM解析器... DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); //定义一个xml文档的输入源.. is.setCharacterStream(new StringReader(bos.toString("UTF-8"))); //设置输入源的字符流... doc = db.parse(is); //指定输入源的内容解析成一个XML文档,并返回一个DOM对象... System.out.println("doc:= "+ doc.getImplementation()); Element root =doc.getDocumentElement(); return_Suc = root.getAttribute("successful"); System.out.println("resSuc:="+return_Suc); //得到文档名称为sendresult的元素的节点列表 NodeList nList = doc.getElementsByTagName("sendresult"); for(int i = 0; i< nList.getLength() ; i ++){ Element node = (Element)nList.item(i); return_msg = node.getElementsByTagName("resultdescription").item(0).getFirstChild().getNodeValue(); //System.out.println("return_msg:"+return_msg); //System.out.println("resultdescription: "+ node.getElementsByTagName("resultdescription").item(0).getFirstChild().getNodeValue()); } } catch (ParserConfigurationException e) { e.getMessage(); } catch (SAXException e) { e.getMessage(); } catch (IOException e) { e.getMessage(); } System.out.println("==================END===================="); //System.out.println("return_Suc1:"+return_Suc); // System.out.println("return_msg1:"+return_msg); return "返回成功与否标记:"+return_Suc+",返回消息:"+return_msg; }catch (IOException e) { e.printStackTrace(); } return "返回成功"; } /** * **/ /** *初始化一个DocumentBuilder *@return a DocumentBuilder *@throws ParserConfigurationException **/ public static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException{ return newDocumentBuilderFactory().newDocumentBuilder(); } /** *初始化一个DocumentBuilderFactory *@return a DocumentBuilderFactory **/ public static DocumentBuilderFactory newDocumentBuilderFactory(){ DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); return dbf; } }
3、调用java程序 以文件流方式传输至用友NC系统指定地址,返回连接状态
FUNCTION cux_java_sendfile(p_addr VARCHAR2,
p_filepath IN VARCHAR2,
p_filename IN VARCHAR2) RETURN VARCHAR2
AS
LANGUAGE JAVA NAME 'cux_java_sendfile.xzSendFile(java.lang.String,java.lang.String,java.lang.String) return java.lang.String';