一、ERP封裝數據成XML寫入數據庫服務器指定文件java
--指定相關文件信息
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;
二、讀取文件,經過java流傳給外圍系統node
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; } }
三、調用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';