「java中接收报文」java接收post

博主:adminadmin 2023-03-17 11:13:11 313

今天给各位分享java中接收报文的知识,其中也会对java接收post进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java的socke client端接收xml报文的问题

一次性读取是不可靠的,一般是定义一个字节数组,用一个循环读取。然后再把读到的数据加起来。主要代码:

InputStream in=null;//你的socket对应的接收流

ByteArrayOutputStream bo=new ByteArrayOutputStream();//用来暂时存放接收到的数据的字节数组流

byte b[]=new byte[100];

int length=0;

while((length=in.read(b))!=-1)

{

bo.write(b,0,length);

}

byte ba[]=bo.toByteArray();

System.out.println(new String(ba));//这里要注意编码,根据实际情况有所变化,不然可能会有乱码

bo.close();

in.close();

java中报文啥时候用呢?一直搞不懂

你可以了解一下http协议

报文就是一组客户端和服务端都认可的信息数据。可以是xml形式,json格式。等等

比如有一个android客户端向服务端发送登陆报文

user

usernamebaidu/username

password123456/password

/user

这段由客户端到服务端的xml,就可以视为一个上行报文

服务端收到报文后会返回登陆成功或者失败的信息

比如:200(成功)

404 (失败)

甚至返回更详细的失败信息等等这个由服务端到客户端的返回信息同样可以是多样性,可视为下行报文

如何用java实现接收trap报文

1. 在net-snmp安装目录下新建snmptrapd.conf文件,假设本系统使用以下路径:/ABC/soft/net-snmp/share/snmp/snmptrapd.conf

2. 在snmptrapd.conf中加入以下指令:

authCommunity log,execute,net public

这条指令指明以“public”为“community”请求的snmp “notification”允许的操作。

各变量意义如下:

log: log the details of the notification - either in a specified file, to standard output (or stderr), or via syslog(or similar).

execute: pass the details of the trap to a specified handler program, including embedded perl.

net: forward the trap to another notification receiver.

Java Socket开发 关于报文传递和接收

看 Oracle 官方教程,同步式的 Socket 就是传统的一问一答方式,它就是你需要的。

客户端先 socket.getOutputStream().write(...); 之后到 socket.getInputStream().read(byte[]) 在循环中读取直到 read 方法返回 -1 或你期望的字节数已经全部收到了就停下来,如果不尝试停下来,后面的 read 将会阻塞等待。

基于性能改进,一般我们需要使用 NIO 异步的 socket,只需要一个线程负责通信,每个线程都有自己的出站消息队列和入站消息队列,以线程为 key 区分开,通信线程只负责把各自的消息从出站队列中发送去并把收到的消息放入入站队列中,应用程序线程就去各自的消息队列中取消息就可以了。因为每个应用线程有各自的消息队列,我们把消息放入出站队列之后就到入站队列上用同步锁等待的方法阻塞到有消息回答时为止。

关于 NIO non-blocking 非阻塞式 socket,下面有一个 NBTimeServer 例子,它讲的是服务端。客户端与此类似,

NIO 通信线程样例。

  public void run()

    {

        int tip = 0;

        try

        {

            selector = Selector.open();

            SelectionKey k = channel.register(selector, getInterestOptions());

            k.attach(thread); // 把当前线程绑定到附件中。

            this.running = true;

            statusChanged(Status.CONNECTED);

            while (this.isRunning())

            {

                // select() is a blocking operation.

                int eventCount = selector.select();

                debug("[MC.Debug] Polling TCP events ... " + eventCount);

                if (eventCount  0  channel.isOpen()  this.isRunning())

                {

                    Set keys = selector.selectedKeys();

                    for (Iterator iter = keys.iterator(); iter.hasNext(); iter.remove())

                    {

                        SelectionKey key = (SelectionKey) iter.next();

                        Thread thread = (Thread) key.attachment();

                        

                        if (!key.isValid())

                        { // channel is closing.

                            break;

                        }

                        process(key); // 处理读取消息并把消息放入 thread 对应的队列。//写出消息类似的,不过在 register 时需要注册写出允许的事件,

                        

                    }

                }

            }

        }

http如何实现同时发送文件和报文(用java实现)

你用的servlet 还是别的框架?

选POST

选form-data

选body

选File

选文件

Send

// commons fileupload组件的情况下,servlet接收的数据只能是type=file表单元素类型,那么获取type=text类型,就可以使用parseRequest(request)来获取list,fileitem,判断isFormField,为true非file类型的。就可以处理了。下面是处理的部分代码:

DiskFileItemFactory factory = new DiskFileItemFactory();factory.setSizeThreshold(1024*1024);

String dirtemp = "c:";

File filedir = new File(dirtemp + "filetemp");

String str = null;if(!filedir.exists())filedir.mkdir();factory.setRepository(filedir);

ServletFileUpload upload = new ServletFileUpload(factory);

List list = upload.parseRequest(request);for(

int i = 0;ilist.size();i++)

{

FileItem item = (FileItem) list.get(i);

if (item.isFormField()) {

System.out.println(item.getString());

} else {

String filename = item.getName();

item.write(new File(request.getRealPath(dir), filename));

}

}

java webservice 做的服务端怎么接收客户端发送的报文xml

String http = "";//请求报文

String str = "";//请求报文中的某一行

public MyHttpServerletRequest(BufferedReader in){

try {

while((str=in.readLine()).length() 0 ){

http += str+"\r\n";//拼接报文

}

} catch (Exception e) {

e.printStackTrace();

}

}

//获得客户端发来的请求报文

public String getHttp(){

return this.http;

}

java中接收报文的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java接收post、java中接收报文的信息别忘了在本站进行查找喔。