资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java接收报文代码 java 报文

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实现接收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 时需要注册写出允许的事件,

}

}

}

}

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接收报文代码 java 报文
分享网址:http://cdkjz.cn/article/hihoih.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220