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安全协议的文件传输管理工具 ...
随机推荐
- Linux硬链接和软连接
硬链接(hard link): A是B的硬链接(A和B都是文件名),则A的目录项中的inode节点号与B的目录项中的inode节点号相同,即一个inode节点对应两个不同的文件名,两个文件名指向同一个 ...
- Nginx 对访问量的控制
目的 了解 Nginx 的 ngx_http_limit_conn_module 和 ngx_http_limit_req_module 模块,对请求访问量进行控制. Nginx 模块化 nginx ...
- vue-waterfall2 基于Vue.js 瀑布流组件
vue-waterfall2 1.宽度自适应,数据绑定特效(适用于上拉加载更多) 2.自定义程度高 3.使用极为简便,适用于PC/移动端 4.提供resize(强制刷新布局-适用于下拉刷新)/mix( ...
- 【水滴石穿】React-Redux-Demo
这个项目没有使用什么组件,可以理解就是个redux项目 项目地址为:https://github.com/HuPingKang/React-Redux-Demo 先看效果图 点击颜色字体颜色改变,以及 ...
- openssl生成SSL证书的流程 - moonhillcity的博客 - CSDN博客
1.安装openssl 之后在/usr/lib/ssl目录下(ubuntu系统,用whereis查下ssl目录即可)下找到openssl.cnf,拷贝到工作目录下. 2.工作目录下新建demoCA文件 ...
- 使用HashMap编写一程序实现存储某班级学生信息
1. 使用HashMap编写一程序实现存储某班级学生信息,要求在屏幕上打印如下列表 学号 姓名 性别 年龄 001 张三 男 23 002 李四 男 ...
- nginx与apache
参考链接:https://www.cnblogs.com/changning0822/p/7844004.html
- Mysql里查询字段为Json格式的数据模糊查询以及分页方法
public void datagrid(CustomFormEntity customForm,HttpServletRequest request, HttpServletResponse res ...
- 远程安装App到手机
注意: 必须是手机和电脑网络连通正常 1. 手机端安装终端模拟器. 2. 打开终端模拟器执行下面命令(也可以在adb shell中执行): su setprop service.adb.tcp.por ...
- win2003开启ftp
首先你要添加IIS,然后才可以启动配置FTP,步骤如下: 1.控制面板→添加或删除程序→添加/删除windows组件: 2.在弹出的windows组件向导窗口中,选择并勾选“应用程序服务器”,然后点击 ...