Netty进行文件传输
本次是利用TCP在客户端发送文件流,服务端就接收流,写入相应的文件。
package net.xjdsz.file;import io.netty.bootstrap.Bootstrap;import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.*;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioSocketChannel;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.InputStream;/*** Created by dingshuo on 2017/7/6.*/public class UploadClient {public static void main(String[] args) throws Exception{UploadClient client=new UploadClient();client.connect();}public void connect(){EventLoopGroup group=new NioEventLoopGroup();try{Bootstrap b=new Bootstrap();b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY,true).handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {ByteBuf msg;InputStream in = new FileInputStream("D:\\Koala.jpg");ByteArrayOutputStream out = new ByteArrayOutputStream();byte[] buffer = new byte[1024 * 100];int n = 0;while ((n = in.read(buffer)) != -1) {msg= Unpooled.buffer(buffer.length);//这里读取到多少,就发送多少,是为了防止最后一次读取没法满填充buffer,//导致将buffer中的处于尾部的上一次遗留数据也发送走msg.writeBytes(buffer,0,n);ctx.writeAndFlush(msg);msg.clear();}System.out.println(n);}});}});ChannelFuture f=b.connect("127.0.0.1",20000).sync();f.channel().closeFuture().sync();}catch (Exception e){}finally {group.shutdownGracefully();}}}
package net.xjdsz.file;import io.netty.bootstrap.ServerBootstrap;import io.netty.buffer.ByteBuf;import io.netty.channel.*;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.logging.LogLevel;import io.netty.handler.logging.LoggingHandler;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;/*** Created by dingshuo on 2017/7/6.*/public class UploadServer {public void bind(int port) throws Exception{EventLoopGroup bossGroup=new NioEventLoopGroup();EventLoopGroup workerGroup=new NioEventLoopGroup();try{ServerBootstrap b=new ServerBootstrap();b.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG,1024).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {super.channelActive(ctx);}@Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception {super.channelInactive(ctx);}@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {String path="D:\\test.jpg";File file=new File(path);if(!file.exists()){file.createNewFile();}FileOutputStream fos=new FileOutputStream(file,true);// BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(fos);ByteBuf buf=(ByteBuf)msg;byte[] bytes=new byte[buf.readableBytes()];buf.readBytes(bytes);System.out.println("本次接收内容长度:" + bytes.length);try {// bufferedOutputStream.write(bytes, 0, bytes.length);// buf.release();fos.write(bytes);fos.flush();} catch (IOException e) {e.printStackTrace();}}});}});//绑定端口,同步等待成功ChannelFuture f=b.bind(port).sync();//等待服务端监听端口关闭f.channel().closeFuture().sync();}finally {//退出,释放资源bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}public static void main(String[] args) throws Exception{UploadServer uploadServer=new UploadServer();uploadServer.bind(20000);}}
Netty进行文件传输的更多相关文章
- netty 文件传输
FileServer package com.zhaowb.netty.ch13_1; import io.netty.bootstrap.ServerBootstrap; import io.net ...
- RPC基于http协议通过netty支持文件上传下载
本人在中间件研发组(主要开发RPC),近期遇到一个需求:RPC基于http协议通过netty支持文件上传下载 经过一系列的资料查找学习,终于实现了该功能 通过netty实现文件上传下载,主要在编解码时 ...
- Linux主机上实现树莓派的交叉编译及文件传输,远程登陆
0.环境 Linux主机OS:Ubuntu14.04 64位,运行在wmware workstation 10虚拟机 树莓派版本:raspberry pi 2 B型. 树莓派OS:官网下的的raspb ...
- putty提供的两个文件传输工具PSCP、PSFTP详细介绍
用 SSH 来传输文件 PuTTY 提供了两个文件传输工具 PSCP (PuTTY Secure Copy client) PSFTP (PuTTY SFTP client) PSCP 通过 SSH ...
- c# 局域网文件传输实例
一个基于c#的点对点局域网文件传输小案例,运行效果截图 //界面窗体 using System;using System.Collections.Generic;using System.Compon ...
- 使用 zssh 进行 Zmodem 文件传输
Zmodem 最早是设计用来在串行连接(uart.rs232.rs485)上进行数据传输的,比如,在 minicom 下,我们就可以方便的用 Zmodem (说 sz .rz 可能大家更熟悉)传输文件 ...
- 在windows 与Linux间实现文件传输(C++&C实现)
要实现windows与linux间的文件传输,可以通过socket网络编程来实现. 这次要实现的功能与<Windows下通过socket进行字符串和文件传输>中实现的功能相同,即客户端首先 ...
- Windows下通过socket进行字符串和文件传输
今天在windows平台下,通过socket实现了简单的文件传输.通过实现这一功能,了解基本的windows网络编程和相关函数的使用方法. 在windows平台上进行网络编程,首先都需要调用函数WSA ...
- Linux下几种文件传输命令 sz rz sftp scp
Linux下几种文件传输命令 sz rz sftp scp 最近在部署系统时接触了一些文件传输命令,分别做一下简单记录: 1.sftp Secure Ftp 是一个基于SSH安全协议的文件传输管理工具 ...
随机推荐
- 2018-8-10-win10-UWP-修改密码框文字水平
title author date CreateTime categories win10 UWP 修改密码框文字水平 lindexi 2018-08-10 19:17:19 +0800 2018-2 ...
- Android中View的layout mechanism(布局机制)
layout mechanism Android中View的layout mechanism主要分为两个阶段:measure阶段和layout阶段.layout mechanism按照一定的顺序进行, ...
- mongodb的一些简单操作
mongo 使用 mongod 开机mongod --dbpath c:\mongo mongod --storageEngine mmapv1 --dbpath c:\mongo mongoimpo ...
- js中+号强制转换小例子
1 <script> console.log(([]+{}).length); </script> </head> 输出竟然是: 为什么会是15呢? 因为在+号的强 ...
- 安装vagrant&virtualBox
https://blog.csdn.net/dream_188810/article/details/78218073 VirtualBox是一款开源免费的虚拟机软件(之前一直使用vm,vm功能较多, ...
- iOS app 设计推荐
见微知著,谈移动缺省页设计 http://www.cocoachina.com/design/20150303/11186.html Facebook产品设计总监!设计APP时的14个必考题 http ...
- 2019-6-23-开源项目使用-appveyor-自动构建
title author date CreateTime categories 开源项目使用 appveyor 自动构建 lindexi 2019-06-23 11:47:40 +0800 2019- ...
- Java练习 SDUT-3338_计算各种图形的周长(接口与多态)
计算各种图形的周长(接口与多态) Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 定义接口Shape,定义求周长的方法l ...
- Java练习 SDUT-2670_3-1 Point类的构造函数
3-1 Point类的构造函数 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 通过本题目的练习可以掌握类的构造函数的定 ...
- HDU 5672 String【尺取法】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5672 题意: 有一个10≤长度≤1,000,000的字符串,仅由小写字母构成.求有多少个子串,包含有 ...