- 浏览: 821252 次
- 性别:
- 来自: 北京、四川
文章分类
最新评论
-
sunbeamzheng:
总结的很好,好好看看。 拷贝问题确实很需要注意,特别是影不影响 ...
java深拷贝与浅拷贝 -
xmh8023:
...
获取POST数据的值 -
xmh8023:
我访问别的服务器怎么办?急求
获取POST数据的值 -
xmh8023:
String urlString="http://l ...
获取POST数据的值 -
lv12312:
Tomcat 7的老版本么?有bug的,https://iss ...
JMX问题
http stream
- 博客分类:
- j2EE
StringBuilder sb = new StringBuilder(); sb.append("HTTP/1.1 200 OK\r\n"); sb.append("Content-Type: text/plain\r\n"); sb.append("Transfer-Encoding: chunked\r\n\r\n"); sb.append("25\r\n"); sb.append("This is the data in the first chunk\r\n"); // 37 bytes sb.append("\r\n1A\r\n"); sb.append("and this is the second one"); // 26 bytes sb.append("\r\n0\r\n\r\n");
十六进制包长+\r\n+报文包+\r\n 为一个传输单元
0+\r\n+\r\n 当遇到这种空传输单元时结束
下面是客户端例子
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.InetSocketAddress; import java.net.Socket; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.FutureTask; public class Client { public boolean isAsync=false; /** * 建立socket * @param ip * @param port * @return * @throws IOException * @throws NumberFormatException * @throws PachiraAsrSocketCreateException */ protected SocketChannel createSocketChannel(String ip,String port) throws NumberFormatException, IOException { SocketChannel socketChannel=null; if(isAsync){ socketChannel = SocketChannel.open(); socketChannel.configureBlocking(false); //向服务端发起连接 if (!socketChannel.connect(new InetSocketAddress(ip, Integer.parseInt(port)))){ //不断地轮询连接状态,直到完成连接 while (!socketChannel.finishConnect()){ //在等待连接的时间里,可以执行其他任务,以充分发挥非阻塞IO的异步特性 //这里为了演示该方法的使用,只是一直打印"." try { Thread.sleep(10); } catch (InterruptedException e) { } // SrvLogger.debug(getClass(), ""); } } }else{ socketChannel = SocketChannel.open(); socketChannel.connect(new InetSocketAddress(ip, Integer.parseInt(port))); } return socketChannel; } /** * 关闭socket * @param socketChannel * @param uuid * @throws IOException */ protected void closeSocketChannel(SocketChannel socketChannel) throws IOException{ if(socketChannel!=null){ socketChannel.close(); } } /** * 传输数据 * @param socket * @param in * @param uuid * @param audioType * @throws IOException */ protected boolean sendStringData(final SocketChannel socketChannel ,final String str) throws IOException{ ByteBuffer buffer=ByteBuffer.wrap(str.getBytes(), 0, str.length()); int size=0; int wl=0; System.out.println("buf.limit="+buffer.limit()); wl=socketChannel.write(buffer); while (buffer.hasRemaining()) { if (wl < 0){ System.out.println("sendData len is -1;size="+size); break; } if (wl == 0) { System.out.println("sendData len is 0 ;size="+size); } size+=wl; } buffer.flip(); return true; } /** * 传输数据 * @param socket * @param in * @param uuid * @param audioType */ protected boolean sendData(final SocketChannel socketChannel ,final InputStream is){ FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>(){ public Integer call() { System.out.println("sendData start...;"); byte[] buf = new byte[8096]; int totalSize=0; int sendTotalSize=0; try { int read = is.read(buf, 0, buf.length); while (read > 0) { totalSize+=read; ByteBuffer buffer=ByteBuffer.wrap(buf, 0, read); int size=0; int wl=0; wl=socketChannel.write(buffer); while (buffer.hasRemaining()) { if (wl < 0){ System.out.println("sendData len is -1;size="+size); break; } if (wl == 0) { System.out.println("sendData len is 0 ;size="+size); } size+=wl; } buffer.flip(); sendTotalSize+=read; read = is.read(buf, 0, buf.length); } sendStringData(socketChannel, "------------V2ymHFg03ehbqgZCaKO6jy--"); System.out.println("sendData end,sendTotalSize="+sendTotalSize+";totalSize="+totalSize); }catch (Exception e) { e.printStackTrace(); }finally{ } return new Integer(8); } }); ExecutorService sendDataPool=Executors.newCachedThreadPool(); sendDataPool.execute(task); return true; } /** * 传输数据 * 十六进制包长+\r\n+报文包+\r\n 为一个传输单元 0+\r\n+\r\n 当遇到这种空传输单元时结束 * @param socket * @param in * @param uuid * @param audioType */ protected boolean sendDataChunk(final SocketChannel socketChannel ,final InputStream is){ FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>(){ public Integer call() throws IOException { System.out.println("sendData start...;"); sendStringData(socketChannel, "\r\n"); String parameter="------------V2ymHFg03ehbqgZCaKO6jy\r\nContent-Disposition: form-data;name=\"chon.wav\";opcode=\"transcribe_audio\";sessionid=\"\";filename=\"chon.wav\";type=\"23\";\r\n\r\n"; sendStringData(socketChannel, Integer.toHexString(parameter.length())+"\r\n"); sendStringData(socketChannel, parameter+"\r\n"); // sendStringData(socketChannel, Integer.toHexString("1234".length())+"\r\n1234\r\n"); // sendStringData(socketChannel, Integer.toHexString("1234".length())+"\r\n1234\r\n"); // sendStringData(socketChannel, Integer.toHexString("1234".length())+"\r\n1234\r\n"); // sendStringData(socketChannel, Integer.toHexString("1234".length())+"\r\n1234\r\n"); // sendStringData(socketChannel, Integer.toHexString("------------V2ymHFg03ehbqgZCaKO6jy".length())+"\r\n"); // sendStringData(socketChannel, "------------V2ymHFg03ehbqgZCaKO6jy\r\n"); // String parameter="Content-Disposition: form-data;name=\"chon.wav\";opcode=\"transcribe_audio\";sessionid=\"\";filename=\"chon.wav\";type=\"23\";"; // sendStringData(socketChannel, Integer.toHexString(parameter.length())+"\r\n"); // sendStringData(socketChannel, parameter+"\r\n"); byte[] buf = new byte[8096]; int totalSize=0; int sendTotalSize=0; try { int read = is.read(buf, 0, buf.length); while (read > 0) { totalSize+=read; ByteBuffer buffer=ByteBuffer.wrap(buf, 0, read); String hex= Integer.toHexString(read); System.out.println("read="+read+";hex="+hex); sendStringData(socketChannel,hex+"\r\n"); int size=0; int wl=0; // System.out.println("send.."); wl=socketChannel.write(buffer); // System.out.println("send..."); while (buffer.hasRemaining()) { if (wl < 0){ System.out.println("sendData len is -1;size="+size); break; } if (wl == 0) { System.out.println("sendData len is 0 ;size="+size); } size+=wl; } sendStringData(socketChannel, "\r\n"); buffer.flip(); sendTotalSize+=read; read = is.read(buf, 0, buf.length); Thread.sleep(50); } sendStringData(socketChannel, Integer.toHexString("------------V2ynHFg03ehbqgZCaKO6jy--".length())+"\r\n"); sendStringData(socketChannel, "------------V2ymHFg03ehbqgZCaKO6jy--"); sendStringData(socketChannel, "\r\n"); sendStringData(socketChannel, "0\r\n\r\n"); System.out.println("sendData end,sendTotalSize="+sendTotalSize+";totalSize="+totalSize); }catch (Exception e) { e.printStackTrace(); }finally{ } return new Integer(8); } }); ExecutorService sendDataPool=Executors.newCachedThreadPool(); sendDataPool.execute(task); return true; } /** * 读取 * @param inputStream * @param buf * @return * @throws IOException */ protected boolean readData(SocketChannel socketChannel, ByteBuffer buf) { boolean ret = true; long count=0; try { count = socketChannel.read(buf); // if(this.isAsync){ while(count<buf.limit()){ if(count==-1){ System.out.println("readData count is -1"); return false; } count += socketChannel.read(buf); try { Thread.sleep(10); } catch (InterruptedException e) { return false; } } // System.out.println("buffer.position()="+buf.position()+";buffer.limit()="+buf.limit()); // System.out.println("count="+count); // } if(count>0){ buf.flip(); } } catch (Exception e) { ret=false; }finally{ System.out.println("readData count="+count+";bufLen="+buf.limit()); } return ret; } /** * 读取 * @param inputStream * @param buf * @return * @throws IOException */ protected boolean readDataBySocket(SocketChannel socketChannel) throws IOException { Socket socket=socketChannel.socket(); InputStream in=socket.getInputStream(); byte[] buf1=new byte[7]; while(this.read(in, buf1)){ System.out.println("result"+new String(buf1)); } return false; } protected boolean read(InputStream inputStream, byte[] buf) throws IOException { boolean ret = true; int totalSize = buf.length; int read = inputStream.read(buf, 0, buf.length); while (read < totalSize) { read += inputStream.read(buf, read, (totalSize - read)); } return ret; } public void nonstream() throws IOException{ String ip="127.0.0.1"; String port="8080"; File file=new File("I:/1/pase/90s_9.wav"); FileInputStream fis=new FileInputStream(file); SocketChannel socketChannel=createSocketChannel(ip, port); String parameter="------------V2ynHFg03ehbqgZCaKO6jy\r\nContent-Disposition: form-data;name=\"chon.wav\";opcode=\"transcribe_audio\";sessionid=\"\";filename=\"chon.wav\";type=\"0\";"; sendStringData(socketChannel, "POST /project/uploader HTTP/1.1\r\n"); sendStringData(socketChannel, "Accept: */*\r\n"); sendStringData(socketChannel, "User-Agent: Mozilla/4.0\r\n"); sendStringData(socketChannel, "Content-Length: "+(file.length()+parameter.length())+"\r\n"); sendStringData(socketChannel, "Accept-Language: en-us\r\n"); sendStringData(socketChannel, "Accept-Encoding: gzip, deflate\r\n"); sendStringData(socketChannel, "Host: 127.0.0.1\r\n"); sendStringData(socketChannel, "Content-Type: multipart/form-data;boundary=----------V2ymHFg03ehbqgZCaKO6jy\r\n"); sendStringData(socketChannel, "\r\n"); sendStringData(socketChannel, parameter+"\r\n"); sendStringData(socketChannel, "\r\n"); //send file1449930 sendData(socketChannel, fis); // client.sendStringData(socketChannel, "------------V2ymHFg03ehbqgZCaKO6jy--"); ByteBuffer bb=ByteBuffer.allocate(2000); readData(socketChannel, bb); byte[] b=new byte[bb.limit()]; bb.get(b, 0, bb.limit()-1); System.out.println(new String(b)); } public static void main(String[] args) throws NumberFormatException, IOException { String ip="localhost"; String port="8080"; Client client=new Client(); // File file=new File("I:/1/a.txt"); File file=new File("I:/1/pase/90s_9.wav"); FileInputStream fis=new FileInputStream(file); SocketChannel socketChannel=client.createSocketChannel(ip, port); client.sendStringData(socketChannel, "POST /project/uploader HTTP/1.1\r\n"); client.sendStringData(socketChannel, "Accept: */*\r\n"); client.sendStringData(socketChannel, "User-Agent: Mozilla/4.0\r\n"); // client.sendStringData(socketChannel, "Content-Length: "+(file.length()+parameter.length())+"\r\n"); client.sendStringData(socketChannel, "Transfer-Encoding: chunked\r\n"); client.sendStringData(socketChannel, "Accept-Language: en-us\r\n"); client.sendStringData(socketChannel, "Accept-Encoding: gzip, deflate\r\n"); client.sendStringData(socketChannel, "Host: 127.0.0.1\r\n"); client.sendStringData(socketChannel, "Content-Type: multipart/form-data;boundary=----------V2ynHFg03ehbqgZCaKO6jy\r\n"); //send file1449930 client.sendDataChunk(socketChannel, fis); while(true){ System.out.println("read start...."); ByteBuffer bb=ByteBuffer.allocate(200); boolean flag=client.readData(socketChannel, bb); byte[] b=new byte[bb.limit()]; bb.get(b, 0, bb.limit()-1); System.out.println(new String(b,"UTF-8")); if(!flag){ System.out.println("socket close...."); client.closeSocketChannel(socketChannel); break; } } System.out.println("read data end...."); System.exit(0); } }
发表评论
-
Java并发编程:volatile关键字解析
2015-07-30 11:30 592转:http://www.cnblogs.com/dolp ... -
Java内存模型
2015-07-29 13:55 8751. 概述 多任务和 ... -
自定义classloader
2015-07-29 13:54 640转:http://tiantian911.iteye.com ... -
自定义ClassLoader实现java应用核心逻辑模块热部署
2015-07-29 13:51 964转:http://blog.csdn.net/zhangda ... -
java classloader原理初探
2015-07-29 10:14 626转:http://www.cnblogs.com/ ... -
Java 内存分配全面浅析
2015-07-29 08:52 572转:http://blog.csdn.net/shimi ... -
Tomcat远程调试
2011-04-09 12:33 1036需要在Tomcat中的catalina.bat中添加如下的一行 ... -
用jmx监控多台服务器(tomcat)
2010-09-08 18:25 4535因为需要写一个后台监控服务器的程序,涉及到jmx,也涉 ... -
call cmd /c start 一点疑问
2010-09-01 10:16 2214call "cmd /c start aaa.bat ... -
Windows计划任务之schtasks
2010-08-30 13:21 3543创建:SCHTASKS /Create /RU SYSTEM ... -
ajax返回值中有中文存在的乱码现象
2010-08-27 16:28 1410ajax返回值中有中文存在的乱码现象,解决就加入下面一行代码即 ... -
MIME TYPE
2010-08-26 16:23 7211最近要做需要在页面上放音频的东西,因此需要用到mime typ ... -
JMX问题
2010-08-20 17:20 5460这个问题貌似是启动tomcat之后就获取不到jmx的链接了,不 ... -
Eclipse控制台乱码
2010-08-05 15:18 4853安装了Eclipse,在运行tomcat时,控制台的中文显示乱 ... -
JMX服务端和客户端的代码
2010-07-27 15:37 3517服务端代码如下 package com.rmi; i ... -
JMX连接Tomcat的JMX测试类
2010-07-27 15:33 3262首先是为了使tomcat支持JMX,必须在tomcat的启动项 ... -
JMX的一个链接类
2010-07-27 09:37 1423package com.pachira.oamp.jmxS ... -
java中文转unicode码
2010-07-22 11:04 43317转载地址:http://www.iteye.com/topic ... -
获取POST数据的值
2010-07-21 14:17 9865当method为POST,Content-Type为 ... -
apache的tomcat负载均衡(两个tomcat)和集群配置(session复制)
2010-06-05 16:24 4553参考地址:http://www.iteye.com/topic ...
相关推荐
安装HTTPStream 托管在 PyPI 上,因此要安装,只需使用pip : pip install httpstream快速开始>>> from httpstream import get>>> get("https://api.duckduckgo.com/?q=neo4j&format=json").content{'Abstract': '...
httpstream:HTTP资源的可靠可读流 httpstream提供了一个Readable流接口(用于Node 0.10及更高版本),该接口通过从上次中断的地方重试请求来抽象化上游的瞬时故障。 httpstream需要Node 0.10或更高版本以及node-...
ios 有三个demo 1.NSURL 下载网络图片(block和delegate两种方式) 2.NSURLSession 下载网络图片,请求json数据(自己封装的网络block) 3.利用 NSstream 来模拟http请求获取数据
VLC 可以播放组播流, 该软件能够根据IGMP joint, leave, quiery report 自动控制VLC的开关。这样可以自适应的减少网络的带宽。 适合与在局域网能搭建视频播放平台。 希望和朋友们进行交流。QQ119818027.
whtepaper-iphone about http live stream(by realwork)
http-客户端-流用法使用 http 请求作为流: var http = require ( 'http-client-stream' )var endpoint = http ( 'http://www.google.com' )var stream = endpoint . createStream ( )process . stdin . pipe ( ...
go-http-stream-reader 使 Go 应用程序能够使用长时间运行的 HTTP 请求流,例如 。 如果出现错误(例如临时网络中断、停止或关闭连接等),它会自动重新连接,并以尊重远程主机退避规则的可扩展方式重新连接。 ...
主要介绍了详解nginx同一端口监听多个域名和同时监听http与https的相关资料,需要的朋友可以参考下
stream.js 是一个很小、完全独立的Javascript类库,它为你提供了一个新的Javascript数据结构:streams.Streams 是一个操作简单的数据结构,很像数组或链接表,但附加了一些非凡的...(http://www.aqee.net/docs/stream/)
主要介绍了利用stream实现一个简单的http下载器的相关资料,需要的朋友可以参考下
How to Get IPC MJPEG Stream via HTTP Commands
Android流媒体协议http播放网络视频,通过URL地址向网络请求数据,然后通过nuplayer播放网络视频
get http_live_stream_architecture from apple developer website
详细介绍了http live stream的原理和mediafilesegmenter mediastreamsegmenter的使用方法。可以通过自己的macbook搭建一套直播和点播的演示环境。
这个流媒体服务器可以支持RTMP和HLS(Live Http Stream) 从安装开始 Nginx的安装参照我之前的这个: http://redstarofsleep.iteye.com/blog/2010391 不同的是在configure的时候需要增加nginx-rtmp-module的支持,下载好...
Adodb.Stream是ADO的Stream对象,提供存取二进制数据或者文本流,从而实现对流的读、写和管理等操作及各种方法的使用。 http://www.abo518.cn
这个流媒体服务器可以支持RTMP和HLS(Live Http Stream)。 nginx配合ffmpeg做流媒体服务器的原理是: nginx通过rtmp模块提供rtmp服务, ffmpeg推送一个rtmp流到nginx, 然后客户端通过访问nginx来收看实时视频流。...
nginx1.20.2
三个码流分析工具Elecard HEVC Analyzer,Elecard StreamEye Tools,H264Visa 两个wireshark lua脚本 用于导出h264,h265裸码流
stream-echo-nginx-module, 用于 Nginx的tcp/流回显模块( ngx_http_echo_module的端口) 电子邮件名称ngx_stream_echo - Nginx/流回波模块( ngx_http_echo模块的端口)目录名称版本概要说明示例 1示例 2示例 3示例 4...