好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java发送报文与接收报文的实例代码

报文(message)是网络中交换与传输的数据单元,即站点一次性要发送的数据块。报文包含了将要发送的完整的数据信息,其长短很不一致,长度不限且可变。

个人理解:从客户端把字符串写入字节数组流中传达至服务端,但是此字符串是XML格式的,然后到了服务端,使用字节数组进行获取该字符串,再获取该字符串的document对象(因为字符串是xml格式的),然后解析获取数据即可。

发送报文

先创建生成报文的方法,添加了xml数据

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

/**

      * @desc 生成xml报文且转换为字符串

      * @return

      */

     public String xmlToString() {

         StringBuffer sendStr = new StringBuffer();

         // 以下为添加的xml信息

         sendStr.append( "<?xml version=\"1.0\" encoding=\"utf-8\"?>" );

 

         sendStr.append( "<request_data>" );

         sendStr.append( "<request_token>BCDBCD</request_token>" );

         sendStr.append( "<request_cardNum>62284801912705</request_cardNum>" );

         sendStr.append( "<request_name>张飞</request_name>" );

         sendStr.append( "<request_pass>123123</request_pass>" );

         sendStr.append( "<request_time>" + new Dates().CurrentYMDHSMTime()+ "</request_time>" );

 

         sendStr.append( "<monery_count>200.00</monery_count>" );

         sendStr.append( "<shop_name>吉野家</shop_name>" );

         sendStr.append( "<shop_id>JYJ</shop_id>" );

         sendStr.append( "<sale_no>" +UUID.randomUUID().toString()+ "</sale_no>" );

         sendStr.append( "</request_data>" );

         // 创建字符串用于返回

         String str = sendStr.toString();

         return str;

     }

将报文xml转为流写入

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

/**

      * @desc 将xml转为流写入

      * @param servletConnection

      * @throws IOException

      */

     public void xmlWriteStream(HttpURLConnection servletConnection) throws IOException {

         // 创建流,写入xml数据

         OutputStream ouput = servletConnection.getOutputStream();

         // 调用方法获取xml字符串

         String str = xmlToString();

         System.out.println(str);

         // 将xml字符串转为btye数组写入流

         ouput.write(str.getBytes( "UTF-8" ));

         ouput.flush(); // 刷新流

         ouput.close(); // 关闭流

     }

创建客户端与服务端的连接并且设置发送报文的一些属性

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

/**

      * @desc 创建客户端与服务端的连接并且设置发送报文的一些属性

      * @return

      * @throws IOException

      */

     public HttpURLConnection cerateServletUrl() throws IOException {

         // 服务端地址

         URL uploadServlet = new URL( "http://localhost:1023/BaoWenServer/xmlServlet.do" );

         // 创建连接

         HttpURLConnection servletConnection = (HttpURLConnection) uploadServlet.openConnection();

         // 设置连接参数

         servletConnection.setRequestMethod( "POST" ); // 请求类型为post

         servletConnection.setDoOutput( true ); // 是否可读

         servletConnection.setDoInput( true ); // 是否可写

         servletConnection.setAllowUserInteraction( true ); // 是否可交互

         return servletConnection;

     }

获取服务端反馈回来的结果

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

/**

      * @desc 获取服务端反馈回来的结果

      * @param servletConnection

      * @throws IOException

      */

     public void getResponseResult(HttpURLConnection servletConnection)

             throws IOException {

    

         System.out.println( "===============**服务端的返回值**==================" );

         // 获取返回的数据

         InputStream inputStream = servletConnection.getInputStream(); //获取反馈回来的流

         //创建一个缓冲读取的reader对象

         BufferedReader reader = new BufferedReader( new InputStreamReader(inputStream));

         //创建一个能够承接返回来值得sb

         StringBuffer sb = new StringBuffer();

  //创建一个能够临时存储读取一行信息的变量

         String strMessage = "" ;

         //开始循环读取返回的流信息

         while ((strMessage = reader.readLine()) != null ) {

             sb.append(strMessage); //将返回的流的信息逐行的压如到sb中

         }

 

         System.out.println( "接收返回值:" + sb);

     }

最后的调用方法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

/**

      * @throws IOException

      * @desc 用于调用方法发送报文的方法

      */

     public void sendMessage() throws IOException {

         try {

             System.out.println( "=================开始发送报文=================" );

             // 建立连接

             HttpURLConnection servletConnection = cerateServletUrl();

             // 写入数据

             xmlWriteStream(servletConnection);

             // 获取返回数据

             getResponseResult(servletConnection);

         } catch (java.net.ConnectException e) {

             System.out.println( "客户端与服务端连接异常,请再次尝试" );

         }

     }

接收报文

服务端使用web项目进行构建
在service中设置编码集

?

1

2

request.setCharacterEncoding( "utf-8" );

         response.setCharacterEncoding( "utf-8" );

获取客户端发送过来的报文

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

//----------------通过request.getInputStream()获取输入的流----------------------

     // 指定每次8kb

     final int BUFFER_SIZE = 8 * 1024 ;

     byte [] buffer = new byte [BUFFER_SIZE];

     // 获取输出流,与客户端的输出流相对应

     ServletInputStream sis = request.getInputStream();

//  System.out.println("sis:"+sis);

            

     int length = 0 ;

     // 创建字节输出流

     ByteArrayOutputStream baos = new ByteArrayOutputStream();

 

     do {

         length = sis.read(buffer);

         if (length > 0 ) {

             // 写入至字节输出流

             baos.write(buffer, 0 , length);

         }

     } while (length * 2 == BUFFER_SIZE);

     // 把字节输出流转为字符串,得到客户端发送的数据,即为报文

     String bodyData = new String(baos.toByteArray());

此时 bodyData 就是客户端发送来的报文数据:

?

1

2

3

4

5

6

7

8

9

10

11

12

<?xml version= "1.0" encoding= "utf-8" ?>

<request_data>

     <request_token>BCDBCD</request_token>

     <request_cardNum> 62284801912705 </request_cardNum>

     <request_name>张飞</request_name>

     <request_pass> 123123 </request_pass>

     <request_time> 2021 年 01 月 25 日 14 : 51 : 52 </request_time>

     <monery_count> 200.00 </monery_count>

     <shop_name>吉野家</shop_name>

     <shop_id>JYJ</shop_id>

     <sale_no>fc4c66dc-b54b- 4052 -89c1-902be7569f18</sale_no>

</request_data>

读该xml可分析数据:张飞在2021年01月25日 14:51:52在商家id为JYJ的吉野家消费了200.00元,本次消费的流水单号为fc4c66dc-b54b-4052-89c1-902be7569f18,使用的卡号为62284801912705,该卡密码为123123(未采用加密),且当前的标识码为BCDBCD

解析报文

客户端发送的报文数据为Xml类型,所以直接用Xml的数据解析方法解析即可

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

// 获取报文之后,开始解析报文,即为解析Xml

     //1.初始化jdk中的用来解析xml的dom工厂

     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

     //2:获得具体的dom解析器

     DocumentBuilder db = dbf.newDocumentBuilder();

     //3: 解析一个xml文档,获得Document对象(根结点)

     InputSource is = new InputSource( new StringReader(bodyData));

        

     Document document = null ;

     try {

         document = db.parse(is); //将流转换成document对象

     } catch (Exception e){           

         return ;

     }

获取完document对象之后,开始解析

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

//通过抓取根节点进而获取子节点

     NodeList list = document.getElementsByTagName( "request_data" );

     Map<String, Object> map = new HashMap<String, Object>(); //将抓取之后获得到的值存在map中

     XmlServiceImpl service = new XmlServiceImpl();

     for ( int i = 0 ; i < list.getLength(); i++) {

         Element element = (Element) list.item(i); //通过item(i)获取根节点下的每一个子节点

         //1.识别码

         String request_token = element.getElementsByTagName( "request_token" ).item( 0 ).getFirstChild().getNodeValue();

         map.put( "request_token" , request_token);

 

         //2.卡号

         String request_cardNum = element.getElementsByTagName( "request_cardNum" ).item( 0 ).getFirstChild().getNodeValue();

         map.put( "request_cardNum" , request_cardNum);

 

         //3.持卡人姓名

         String request_name = element.getElementsByTagName( "request_name" ).item( 0 ).getFirstChild().getNodeValue();

         map.put( "request_name" , request_name);

 

         //4.该卡的密码

         String request_pass = element.getElementsByTagName( "request_pass" ).item( 0 ).getFirstChild().getNodeValue();

         map.put( "request_pass" , request_pass);

 

         //5.本次消费请求的时间

         String request_time = element.getElementsByTagName( "request_time" ).item( 0 ).getFirstChild().getNodeValue();

         map.put( "request_time" , request_time);

                

         //6.本次消费的金额

         String monery_count = element.getElementsByTagName( "monery_count" ).item( 0 ).getFirstChild().getNodeValue();

         map.put( "monery_count" , monery_count);

                

         

         //7.本次消费到的商家的名字

         String shop_name = element.getElementsByTagName( "shop_name" ).item( 0 ).getFirstChild().getNodeValue();

         map.put( "shop_name" , shop_name);

                                    

                

         //8.本次消费到的商家的id

         String shop_id = element.getElementsByTagName( "shop_id" ).item( 0 ).getFirstChild().getNodeValue();

         map.put( "shop_id" , shop_id);

                                

         //9.本次消费到的流水单号

         String sale_no = element.getElementsByTagName( "sale_no" ).item( 0 ).getFirstChild().getNodeValue();

         map.put( "sale_no" , sale_no);                       

     }

此时输出map对象,查看数据

?

1

{request_name=张飞, shop_id=JYJ, request_time= 2021 年 01 月 25 日 14 : 51 : 52 , request_token=BCDBCD, monery_count= 200.00 , sale_no=fc4c66dc-b54b- 4052 -89c1-902be7569f18, request_cardNum= 62284801912705 , shop_name=吉野家, request_pass= 123123 }

返回报文

一切无误后,返回报文

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

// 要返回的报文

     StringBuffer resultBuffer = new StringBuffer();

     resultBuffer.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );

     resultBuffer.append( "<request_data>" );

     resultBuffer.append( "<request_name>" +request_name+ "</request_name>" );

     resultBuffer.append( "<request_cardNum>" +request_cardNum+ "</request_cardNum>" );

     resultBuffer.append( "<request_time>" + new Dates().CurrentYMDHSMTime()+ "</request_time>" );

     resultBuffer.append( "<shop_name>" +shop_name+ "</shop_name>" );

     resultBuffer.append( "<sale_no>" +sale_no+ "</sale_no>" );

     resultBuffer.append( "<request_token>成功了</request_token>" );

     resultBuffer.append( "</request_data>" );

      // 设置发送报文的格式

     response.setContentType( "text/xml" );

     response.setCharacterEncoding( "UTF-8" );

                         

     PrintWriter out = response.getWriter();

     out.println(resultBuffer.toString());

     out.flush();

     out.close();

到此这篇关于Java发送报文与接收报文的文章就介绍到这了,更多相关Java发送报文与接收报文内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/lolly1023/article/details/113112403

查看更多关于Java发送报文与接收报文的实例代码的详细内容...

  阅读:20次