Java 使用StringBuffer注意
Stringbuffer使用注意
package com.dafsdfsd.core.service; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.Socket; import java.net.UnknownHostException; import org.apache.log4j.Logger; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.junit.Test; import com.dafsdfsd.core.common.PorpertiesConfig; import com.dafsdfsd.core.utils.StringUtils; import com.dafsdfsd.core.utils.xml.XmlUtil4Jdom; public class SendMessageToCore { private static Logger logger = Logger.getLogger(SendMessageToCore.class); /** * 核心返回报文中状态代码节点 */ public static final String MESSAGE_STATUS_CODE = "MessageStatusCode"; /** * 核心返回报文中状态描述节点 */ public static final String MESSAGE_STATUS_DESCRIPTION = "MessageStatusDescription"; /** * 核心返回报文中表示成功的代码 */ public static final String CORE_RETURN_CODE_SUCCESS = "0001"; /** * 发送报文到核心系统<br><pre> * 核心接收GBK编码的报文,在调用这个方法之前应该将报文转换为GBK的编码 <br> * 创建时间:2013-5-30 下午04:21:15 </pre> * @param message 发送到核心的报文 * @return String 核心返回的报文 * @throws 异常类型 说明 */ public static String sendMessage(String message) throws Exception { logger.info("收到的报文内容是:" + message); String coreAddress = PorpertiesConfig.getProperty("core.socketAddress"); String corePortString = PorpertiesConfig.getProperty("core.socketPort"); Socket socket = null; PrintWriter out = null; BufferedReader in = null; BufferedWriter bout = new BufferedWriter(new FileWriter(new File("G:/test.txt"))); BufferedWriter bout1 = new BufferedWriter(new FileWriter(new File("G:/test1.txt"))); // 收到的报文 StringBuffer stringBuffer = new StringBuffer(); String receiveString = null; if (coreAddress != null && corePortString != null) { int corePort = Integer.parseInt(corePortString); try { socket = new Socket(coreAddress, corePort); out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); out.println(message); out.flush(); in = new BufferedReader(new InputStreamReader(socket.getInputStream(), XmlUtil4Jdom.GBK_ENCODING)); while ((message = in.readLine()) != null) { // bout.write(message); stringBuffer.append(message + "\n"); // stringBuffer.append(message); } // BufferedReader流已指定编码输出,这里不需要再次转码了 // receiveString = new String(stringBuffer.toString().getBytes(XmlUtil4Jdom.GBK_ENCODING), XmlUtil4Jdom.GBK_ENCODING); receiveString = stringBuffer.toString(); bout.write(receiveString); } catch (UnknownHostException e) { logger.error("没有找到核心的服务器", e); throw new Exception("没有找到核心的服务器", e); } catch (IOException e) { logger.error("与核心服务器通信出现异常", e); throw new Exception("与核心服务器通信出现异常", e); } finally { if (out != null) out.close(); if (in != null) in.close(); if (bout != null) bout.close(); } } // receiveString = stringBuffer.toString(); // logger.info("核心返回的原始内容是:" + receiveString); String msgString = null; if(null != receiveString && !receiveString.isEmpty()){ msgString = getXmlContent(receiveString); // int msgLength = Integer.parseInt(receiveString.substring(0, 6).trim()); // 前12位为报文长度和接口代码 // msgString = substring4CharArray(receiveString, 12, msgLength); // msgString = receiveString.replaceFirst(msgLength, ""); // msgString = receiveString.substring(12); // byte[] msg = msgString.getBytes(XmlUtil4Jdom.GBK_ENCODING); // // byte[] newMsg = new byte[msgLength]; // for (int i = 0; i < msgLength; i++) { // newMsg[i] = msg[i]; // } // // msgString = new String(newMsg // logger.info("将核心返回的报文内容做编码转换及长度截取之后的结果:" + msgString); logger.info("将核心返回的报文内容做编码转换及长度截取之后的结果:已处理"); } try { bout1.write(msgString); if (bout1 != null) bout1.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Document doc = DocumentHelper.parseText(msgString); return msgString; } public static String getXmlContent(String src) { // StringBuffer sb = new StringBuffer(); String rs = null; try { // byte[] byteArray = null; // char[] charArray = null; // byteArray = src.getBytes(XmlUtil4Jdom.GBK_ENCODING); // charArray = src.toCharArray(); // System.out.println(src.length()); // System.out.println(charArray.length); // System.out.println(byteArray.length); // System.out.println(src.charAt(1)); // System.out.println(src.charAt(src.length() - 2)); // System.out.println(src.charAt(src.length() - 1)); // 获取代码内容 String srcCode = org.apache.commons.lang.StringUtils.substring(src, 0, 12); System.out.println(srcCode); // 获取xml内容 rs = org.apache.commons.lang.StringUtils.substring(src, 12, src.lastIndexOf(">") + 1); // return src; // charArray = src.toCharArray(); // for (int i = 0; i < charArray.length; i++) { // sb.append(charArray[i]); // } // System.out.println(); // System.out.println(); // System.out.println(); // System.out.println(src.length()); // System.out.println(charArray.length); // System.out.println(byteArray.length); //String // for (int i = 0; i < src.length(); i++) { //// sb.append(charArray[i]); //// sb.append(src.charAt(i)); // char s = src.charAt(i); //// System.out.println(s); // sb.append(s); // } //byte // for (int i = 0; i < byteArray.length; i++) { // sb.append((char)byteArray[i]); // } // return new String(byteArray, XmlUtil4Jdom.GBK_ENCODING); // System.out.println(); // System.out.println(); // System.out.println(); // System.out.println(src.charAt(src.length() - 3)); // System.out.println(src.charAt(src.length() - 2)); // System.out.println(src.charAt(src.length() - 1)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs; } @Test public void test() { StringBuffer buffer = new StringBuffer(); FileReader fr = null; BufferedReader br = null; try { // fr = new FileReader("E:/workspace3/i-myproject/b_LOG/temp.xml"); fr = new FileReader("G:/workspace/i-myproject/temp.xml"); br = new BufferedReader(fr); String content = br.readLine(); while (content != null) { buffer.append(content); content = br.readLine(); } String msg = buffer.toString(); sendMessage(new String(msg.getBytes(), XmlUtil4Jdom.GBK_ENCODING)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { br.close(); fr.close(); } catch (IOException e) { e.printStackTrace(); } } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。