参考: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接收大文件的方法的更多相关文章

  1. Linux培训教程 linux系统下分割大文件的方法

    在linux中分割大文件,比如一个5gb日志文件,需要把它分成多个小文件,分割后以利于普通的文本编辑器读取. 有时,需要传输20gb的大文件,Linux培训 教程件到另一台服务器,也需要把它分割成多个 ...

  2. php中读取大文件实现方法详解

    php中读取大文件实现方法详解 来源:   时间:2013-09-05 19:27:01   阅读数:6186 分享到:0 [导读] 本文章来给各位同学介绍php中读取大文件实现方法详解吧,有需要了解 ...

  3. Netty 5 传送大文件的方法

    Netty 5 提供了 一个ChunkedWriterHandler 来传送大文件,发送端如下: 添加chunedwriterhandler: ChannelPipeline p = ...; p.a ...

  4. python tcp黏包和struct模块解决方法,大文件传输方法及MD5校验

    一.TCP协议 粘包现象 和解决方案 黏包现象让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd)执行远程命令的模块 需要用到模块subprocess sub ...

  5. Netty接收HTTP文件上传及文件下载

    文件上传 这个处理器的原理是接收HttpObject对象,按照HttpRequest,HttpContent来做处理,文件内容是在HttpContent消息带来的. 然后在HttpContent中一个 ...

  6. 转(Response.WriteFile 无法下载大文件解决方法)

    以前用Response.WriteFile(filename),但当遇到大文件时无法完整下载. 该方法最大的问题,它不是直接将数据抛到客户端,而是在服务器端(IIS)上缓存.当下载文件比较大时,服务器 ...

  7. 大文件读取方法(C#)

    之前都是用StreamReader.ReadLine方法逐行读取文件,自从.NET4有了File.ReadLines这一利器,就再也不用为大文件发愁了. File.ReadLines在整个文件读取到内 ...

  8. python读取大文件的方法及mmap内存映射模块

    python计算文件的行数和读取某一行内容的实现方法 :最简单的办法是把文件读入一个大的列表中,然后统计列表的长度.如果文件的路径是以参数的形式filepath传递的,那么只用一行代码就可以完成我们的 ...

  9. 把大象装进冰箱:HTTP传输大文件的方法

    上次我们谈到了HTTP报文里的div,知道了HTTP可以传输很多种类的数据,不仅是文本,也能传输图片,音频和视频.   早期互联网上传输的基本上都是只有几k大小的文本和小图片,现在的情况则大有不同.网 ...

随机推荐

  1. Mysql 修改数据库,mysql修改表类型,Mysql增加表字段,Mysql删除表字段,Mysql修改字段名,Mysql修改字段排列顺序,Mysql修改表名

    对于已经创建好的表,尤其是已经有大量数据的表,如果需要对表做一些结构上的改变,我们可以先将表删除(drop),然后再按照新的表定义重建表.这样做没有问题,但是必然要做一些额外的工作,比如数据的重新加载 ...

  2. AutoFac文档12(转载)

    目录 开始 Registering components 控制范围和生命周期 用模块结构化Autofac xml配置 与.net集成 深入理解Autofac 指导 关于 词汇表 Resolve的参数 ...

  3. AutoFac文档6(转载)

    目录 开始 Registering components 控制作用域和生命周期 用模块结构化Autofac xml配置 与.net集成 深入理解Autofac 指导 关于 词汇表 实例生命周期 实例生 ...

  4. 大数据量下MySQL插入方法的性能比较

    不管是日常业务数据处理中,还是数据库的导入导出,都可能遇到需要处理大量数据的插入.插入的方式和数据库引擎都会对插入速度造成影响,这篇文章旨在从理论和实践上对各种方法进行分析和比较,方便以后应用中插入方 ...

  5. angularAMD快速入门

    ngularAMD是作者 marcoslin 使用 RequireJS + AngularJS开发的前端mvvm框架,因此你可以使用它快速创建一款Web App.他特别适合快速开发SPA应用,适当的和 ...

  6. 带密匙的php加密解密代码

    php加密解密示例,代码如下: <?php $id = "http://www.jbxue.com"; $token = encrypt($id, 'E', 'jbxue') ...

  7. jquery操作复选框(checkbox)十二技巧

    jquery操作复选框(checkbox)的12个小技巧. 1.获取单个checkbox选中项(三种写法)$("input:checkbox:checked").val()或者$( ...

  8. CXAnimation类

    #include "XAnimation.h" CXAnimation::CXAnimation(void) { m_strName = ""; m_nFram ...

  9. 17. Subsets【medium】

    Given a set of distinct integers, return all possible subsets. Notice Elements in a subset must be i ...

  10. 重置 ckeditor清空内容

    转载自:http://blog.csdn.net/woshirongshaolin/article/details/8239407 <script type="text/javascr ...