日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

netty接收大文件的方法

發(fā)布時(shí)間:2023/12/18 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 netty接收大文件的方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

參考:http://blog.csdn.net/linuu/article/details/51371595

https://www.jianshu.com/p/a0a51fd79f62

netty默認(rèn)是只能接收1024個(gè)字節(jié),但是我們要傳輸大文件怎么辦?

上代碼:

改之后服務(wù)端:

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; /**用于分配處理業(yè)務(wù)線程的線程組個(gè)數(shù) */ protected static final int BIZGROUPSIZE = Runtime.getRuntime().availableProcessors()*2; //默認(rèn) /** 業(yè)務(wù)出現(xiàn)線程大小*/ protected static final int BIZTHREADSIZE = 4; /* * NioEventLoopGroup實(shí)際上就是個(gè)線程池, * NioEventLoopGroup在后臺(tái)啟動(dòng)了n個(gè)NioEventLoop來(lái)處理Channel事件, * 每一個(gè)NioEventLoop負(fù)責(zé)處理m個(gè)Channel, * NioEventLoopGroup從NioEventLoop數(shù)組里挨個(gè)取出NioEventLoop來(lái)處理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服務(wù)器已啟動(dòng)"); } 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); } } }

改之后客戶(hù)端:

?

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; /** * * 發(fā)送報(bào)文給客戶(hù)端 * * * @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||深圳市福田區(qū)人力資源服務(wù)中心||001#$PM_BHHM||83456517||001#$PM_DJQYDZ||福田區(qū)福強(qiáng)路深圳文化創(chuàng)意園世紀(jì)工藝品文化廣場(chǎng)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號(hào)百信息服務(wù)中心10800010122810070411400006徐冬生115000088291816511600110114+企業(yè)名片行業(yè)版包月套餐,114+短信名片包月套餐_定價(jià)計(jì)劃,114+企業(yè)名片行業(yè)版包月套餐贈(zèng)送3個(gè)月套餐外等額話費(fèi)優(yōu)惠11700017755KH000293285120\t"; String x=SendClient.send("127.0.0.1", 10001, msg, 3500); System.out.println("return string:"+x); } }

處理類(lèi):

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 : 創(chuàng)建響應(yīng)報(bào)文 */ 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

?服務(wù)端增加了:

ByteBuf delimiter = Unpooled.copiedBuffer("\t".getBytes());pipeline.addLast("framer", new DelimiterBasedFrameDecoder(2048,delimiter));

客戶(hù)端報(bào)文增加了

\t

?

轉(zhuǎn)載于:https://www.cnblogs.com/JAYIT/p/7027533.html

總結(jié)

以上是生活随笔為你收集整理的netty接收大文件的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 91豆花视频 | 波多野结衣视频一区 | 91中出| 色网在线观看 | 亚洲国产精品久久精品怡红院 | 西西4444www大胆无码 | 欧美午夜小视频 | 九一国产在线观看 | 少妇久久久久 | 男人天堂亚洲天堂 | 国产xx在线观看 | 一区二区在线免费 | 综合网五月 | 在线免费成人 | 日本电影大尺度免费观看 | 97精品熟女少妇一区二区三区 | 国产不卡视频 | 国产乱码精品一区二区三区五月婷 | 国产一级性生活 | 在线观看www.| 欧美一区二区三区四 | 亚洲五月六月 | 欧美精品激情视频 | 亚洲成年人网站在线观看 | 少妇一区二区三区四区 | 成人av一区二区三区在线观看 | 一级片免费在线观看 | 黄视频免费在线观看 | 国产精品一区二区在线 | 男女超碰 | 国产精品九色 | 久久黄色 | 自偷自拍av| 中文字幕一区av | 91精品视频网站 | 日韩精品一区二区三区免费视频 | 国产肥白大熟妇bbbb视频 | 亚洲一本在线 | 黄网在线观看视频 | 在线视频毛片 | 丰满秘书被猛烈进入高清播放在 | 东方av在线免费观看 | 老湿机69福利区午夜x片 | 亚洲国产123 | 天天摸夜夜 | 一级黄色免费看 | 麻豆传媒在线免费 | 人妻精油按摩bd高清中文字幕 | 熟女性饥渴一区二区三区 | 娇妻高潮浓精白浆xxⅹ | 日本少妇喂奶漫画 | 亚洲成av人片 | 亚洲精品乱码久久久久久写真 | 蜜臀久久99精品久久久久久 | 成人做受视频试看60秒 | 999免费视频 | 让男按摩师摸好爽视频 | 99在线视频播放 | 日韩视频久久 | 国产精品综合视频 | 国产精品88久久久久久妇女 | 黄黄的网站 | 日韩极品在线观看 | 91精品视频免费 | 久久久久人妻一区精品色 | 天天曰天天 | ktv做爰视频一区二区 | 五月婷婷激情视频 | 久久大香| 国产精品18久久久久久无码 | 岛国裸体写真hd在线 | 国产男女激情 | 日韩在线www| 国产精品爱啪在线线免费观看 | 亚洲一级免费毛片 | 日韩亚洲欧美在线 | 欧美大肚乱孕交hd孕妇 | 精品欧美在线 | 熟妇高潮一区二区三区 | 国产无遮挡aaa片爽爽 | 91中文 | av一起看香蕉 | 成品短视频泡芙 | www.色婷婷.com | 亚洲熟妇丰满大屁股熟妇 | av中文字幕网址 | 欧美人xxxx| 色屁屁一区二区 | 国产又粗又猛又爽又黄的视频一 | 国产又大又黄的视频 | 免费一级片视频 | 国产免费午夜 | 一区在线不卡 | 狂野少女电影在线观看国语版免费 | 狠狠干夜夜爽 | 久久高清内射无套 | 日本黄a三级三级三级 | 国产男男chinese网站 | 婷婷影院在线观看 |