netty接收大文件的方法
参考:http://blog.csdn.net/linuu/article/details/51371595
https://www.jianshu.com/p/a0a51fd79f62
netty默认是只能接收1024个字节,但是我们要传输大文件怎么办?
上代码:
改之后服务端:
package server;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import java.nio.charset.Charset;
import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.eshore.ismp.hbinterface.service.BizCommonService;
import com.eshore.ismp.hbinterface.util.ConfigLoadUtil;
public class SpsServer {
private static final Logger logger = Logger.getLogger(SpsServer.class);
private static int PORT = 10001;
/**用于分配处理业务线程的线程组个数 */
protected static final int BIZGROUPSIZE = Runtime.getRuntime().availableProcessors()*2; //默认
/** 业务出现线程大小*/
protected static final int BIZTHREADSIZE = 4;
/*
* NioEventLoopGroup实际上就是个线程池,
* NioEventLoopGroup在后台启动了n个NioEventLoop来处理Channel事件,
* 每一个NioEventLoop负责处理m个Channel,
* NioEventLoopGroup从NioEventLoop数组里挨个取出NioEventLoop来处理Channel
*/
private static final EventLoopGroup bossGroup = new NioEventLoopGroup(BIZGROUPSIZE);
private static final EventLoopGroup workerGroup = new NioEventLoopGroup(BIZTHREADSIZE);
protected static void run(final BizCommonService bizCommonService) throws Exception {
String PORTs=ConfigLoadUtil.getValue("toSpsServerPort");
PORT=Integer.parseInt(PORTs);
logger.info("PORT IS:"+PORT);
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup);
b.channel(NioServerSocketChannel.class);
b.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
/* pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8)); */
ByteBuf delimiter = Unpooled.copiedBuffer("\t".getBytes());
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(2048,delimiter));
pipeline.addLast("decoder", new StringDecoder(Charset.forName("GBK")));
pipeline.addLast("encoder", new StringEncoder(Charset.forName("GBK")));
pipeline.addLast(new SpsServerHandler(bizCommonService));
}
});
b.bind(PORT).sync();
logger.info("TCP服务器已启动");
}
protected static void shutdown() {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
public static void main(String[] args) throws Exception {
try{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
context.start();
BizCommonService bizCommonService = (BizCommonService) context.getBean("bizCommonService");
SpsServer.run(bizCommonService);
}catch(Exception e){
logger.error("start sps interface server error:",e);
System.exit(-1);
}
}
}
改之后客户端:
package fourNoBlocking;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
/**
*
* 发送报文给客户端
*
*
* @date 2016年12月14日 上午11:56:27
* @since 1.0
*/
public class SendClient {
private static final String ENCODING = "GBK";
public static String send(String ip, int port, String sendStr, int timeout) {
long start = System.currentTimeMillis();
System.out.println(sendStr.length());
if (sendStr == null || "".equals(sendStr)) {
return "str is null";
}
Socket client = null;
OutputStream stream = null;
InputStream is = null;
try {
client = new Socket();
InetSocketAddress address = new InetSocketAddress(ip, port);
client.connect(address);
timeout = timeout >= 0 ? timeout : 3500;
client.setSoTimeout(timeout);
stream = client.getOutputStream();
is = client.getInputStream();
int len = 0;
len = sendStr.getBytes(ENCODING).length;
ByteBuffer buf = ByteBuffer.allocate(len);
byte[] bytes = sendStr.getBytes(ENCODING);
buf.put(bytes);
stream.write(buf.array(), 0, len);
stream.flush();
String res = "";
int i = 0;
byte[] b = new byte[6555];
while ((i = is.read(b)) != -1) {
res = new String(b, 0, i);
System.out.println(res);
break;
}
long end = System.currentTimeMillis();
return res;
} catch (Exception e) {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("error send message").append(e.getMessage()).append("&errorID=")
.append(System.currentTimeMillis());
return strBuilder.toString();
} finally {
if (client != null) {
try {
client.close();
} catch (IOException e) {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("error send message").append(e.getMessage()).append("&errorID=")
.append(System.currentTimeMillis());
}
}
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("error send message").append(e.getMessage()).append("&errorID=")
.append(System.currentTimeMillis());
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("error send message").append(e.getMessage()).append("&errorID=")
.append(System.currentTimeMillis());
}
}
}
}
public static void main(String[] args) {
String msg="";
msg="FFFF76623634010100102700170103IBSS017555 000000021800100023402287248808*766236340100200001178400003001785000030217860000302110000004075510100020SZ2000000054121442461020001241324186148310300593PM_DJDHHM||83456517||001#$PM_HYLX||0||001#$BA_MSMAN||海豚||001#$PM_DJQYYB||518000||001#$PM_DJQYMC||深圳市福田区人力资源服务中心||001#$PM_BHHM||83456517||001#$PM_DJQYDZ||福田区福强路深圳文化创意园世纪工艺品文化广场309栋B座1-3层||001#$PM_SFZDXY||XY02||001#$PM_DJKHXX||||001#$BA_MSDEPTNAME||12||001#$PM_DLS||DSL6||001#$PM_YWSLLB||SLLB01||001#$PM_SLDYSLSH||0||001#$PM_JFQ||01||001#$PM_DJHMGS||1||001#$PM_SRFJ||2||001#$PM_JFJG||1||001#$PM_YZ||30||001#$PM_DXFSSL||100||001#$PB_BILLINGTYPE||000000||005#$PB_USERTYPE||100002||005#$PB_USERCHAR||JFSX01||005#$BEGIN_DATE||20170607||005#$END_DATE||||005#$10400014DXMP214688722910700016号百信息服务中心10800010122810070411400006徐冬生115000088291816511600110114+企业名片行业版包月套餐,114+短信名片包月套餐_定价计划,114+企业名片行业版包月套餐赠送3个月套餐外等额话费优惠11700017755KH000293285120\t";
String x=SendClient.send("127.0.0.1", 10001, msg, 3500);
System.out.println("return string:"+x);
}
}
处理类:
package server;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.eshore.ismp.hbinterface.service.BizCommonService;
public class SpsServerHandler extends SimpleChannelInboundHandler<Object> {
private static final Logger logger = LoggerFactory.getLogger(SpsServerHandler.class);
private BizCommonService bizCommonService;
public SpsServerHandler(){}
public SpsServerHandler(BizCommonService bizCommonService){
this.bizCommonService=bizCommonService;
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
logger.info("SERVER接收到消息 msg:{}",msg);
long start = System.currentTimeMillis();
boolean result = bizCommonService.sendOperToCacheAysn(String.valueOf(msg));
/**
* step 3 : 创建响应报文
*/
String res = bizCommonService.createResponseStr(String.valueOf(msg),result);
long end = System.currentTimeMillis();
logger.debug("SpsServer request:{} res:{} time cost:{}ms",String.valueOf(msg),res,(end-start));
ctx.channel().writeAndFlush(res);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx,
Throwable cause) throws Exception {
logger.warn("Unexpected exception from downstream.", cause);
ctx.close();
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
logger.info("client closed:"+ctx.channel().hashCode());
super.channelInactive(ctx);
}
}
输出:
length:1027
服务端增加了:
ByteBuf delimiter = Unpooled.copiedBuffer("\t".getBytes());
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(2048,delimiter));
客户端报文增加了
\t
netty接收大文件的方法的更多相关文章
- Linux培训教程 linux系统下分割大文件的方法
在linux中分割大文件,比如一个5gb日志文件,需要把它分成多个小文件,分割后以利于普通的文本编辑器读取. 有时,需要传输20gb的大文件,Linux培训 教程件到另一台服务器,也需要把它分割成多个 ...
- php中读取大文件实现方法详解
php中读取大文件实现方法详解 来源: 时间:2013-09-05 19:27:01 阅读数:6186 分享到:0 [导读] 本文章来给各位同学介绍php中读取大文件实现方法详解吧,有需要了解 ...
- Netty 5 传送大文件的方法
Netty 5 提供了 一个ChunkedWriterHandler 来传送大文件,发送端如下: 添加chunedwriterhandler: ChannelPipeline p = ...; p.a ...
- python tcp黏包和struct模块解决方法,大文件传输方法及MD5校验
一.TCP协议 粘包现象 和解决方案 黏包现象让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd)执行远程命令的模块 需要用到模块subprocess sub ...
- Netty接收HTTP文件上传及文件下载
文件上传 这个处理器的原理是接收HttpObject对象,按照HttpRequest,HttpContent来做处理,文件内容是在HttpContent消息带来的. 然后在HttpContent中一个 ...
- 转(Response.WriteFile 无法下载大文件解决方法)
以前用Response.WriteFile(filename),但当遇到大文件时无法完整下载. 该方法最大的问题,它不是直接将数据抛到客户端,而是在服务器端(IIS)上缓存.当下载文件比较大时,服务器 ...
- 大文件读取方法(C#)
之前都是用StreamReader.ReadLine方法逐行读取文件,自从.NET4有了File.ReadLines这一利器,就再也不用为大文件发愁了. File.ReadLines在整个文件读取到内 ...
- python读取大文件的方法及mmap内存映射模块
python计算文件的行数和读取某一行内容的实现方法 :最简单的办法是把文件读入一个大的列表中,然后统计列表的长度.如果文件的路径是以参数的形式filepath传递的,那么只用一行代码就可以完成我们的 ...
- 把大象装进冰箱:HTTP传输大文件的方法
上次我们谈到了HTTP报文里的div,知道了HTTP可以传输很多种类的数据,不仅是文本,也能传输图片,音频和视频. 早期互联网上传输的基本上都是只有几k大小的文本和小图片,现在的情况则大有不同.网 ...
随机推荐
- Dev属性设置
DisplayFormat 设置显示格式如:{0:P}表示显示为百分号模式.如数据源中为0.5.表示出来为50% 2.GridContro总合计及分组合计: 常规总合计直接RunDesigner-Gr ...
- # mysqlbinlog mysql-bin.000004 mysqlbinlog: unknown variable 'default-character-set=utf8'
# mysqlbinlog mysql-bin.000004 mysqlbinlog: unknown variable 'default-character-set=utf8' 加上--no-def ...
- Vim-复制选中内容至系统剪贴板,光标移动到指定行的行首和行尾
1.全选并复制到系统剪贴板 ggVG或ggvG 然后 "+y gg 让光标移到首行,在vim才有效,vi中无效 V 是进入Visual(可视)模式 G 光标移到最后一行 "+y 复 ...
- Google 商店:您的应用静态链接到的 OpenSSL 版本有多个安全漏洞。建议您尽快更新 OpenSSL
安全提醒 您的应用静态链接到的 OpenSSL 版本有多个安全漏洞.建议您尽快更新 OpenSSL. 在开头为 1.0.1h.1.0.0m和 0.9.8za的 OpenSSL 版本中这些漏洞已得到修复 ...
- jvm垃圾收集器之Throughput GC
呃.HotSpot VM的GC组老人之一Jon Masamitsu很久之前就写过blog讲解这个:https://blogs.oracle.com/jonthecollector/entry/our_ ...
- 关于浏览器对静态HTML页面的缓存问题
症状: 刚才为了测试TOMCAT的BASIC安全验证,修改了tomcat-users.xml和/WEB-INF/web.xml之后进行测试,<url-pattern>/*<url-p ...
- [dts]TI-am437x dts
imx6 可以参考http://blog.csdn.net/shengzhadon/article/details/49908439 参照文件: Documentation/devicetree/bi ...
- SAN和NAS的区别: 层次不一样
SAN : STORAGE AREA NETWORK 存储区域网络 NAS : NETWORK ATTACHED STORAGE 网络附加存储 NAS不一定是盘阵,一台普通的主机就可以做出NAS, ...
- 如何从CentOS官网下载我们想要的版本
今天想从官网下载6.5版本的CentOS,结果找了好一会儿才找到,赶紧记录下来,以备以后查询. 第一步在百度搜索centos,点击"Download CentOS",如下图所示. ...
- Easyui Datagrid相同连续列合并扩展(三)
function MergeCells(seletor, rows, fields) { if(rows == null || rows.length == 0 || fields == null | ...