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>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
@Override
public 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>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
}
@Override
public 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安全协议的文件传输管理工具 ...
随机推荐
- Python3.7.4入门-4模块
4 模块 Python有一种方法可以把定义放在一个文件里,并在脚本或解释器的交互式实例中使用它们.这样的文件被称作 模块 :模块中的定义可以 导入 到其它模块或者 主 模块 模块是一个包含Python ...
- Linux 7.X 网络配置
Linux 7.X 网络配置 环境: 笔记本中安装了虚拟机,在虚拟机中安装了Redhat 7.4版本的操作系统,现配置该操作系统网络.(IP.网关等) 相关指令如下: # nmcli connecti ...
- Directx11教程(15) D3D11管线(4)
原文:Directx11教程(15) D3D11管线(4) 本章我们首先了解一下D3D11中的逻辑管线,认识一下管线中每个stage的含义. 参考资料:http://fgiesen.wordpress ...
- 使用 Windows 10 WSL 搭建 ESP8266 编译环境并使用 VSCODE 编程(一)(2019-08-23)
目录 使用 Windows 10 WSL 搭建 ESP8266 编译环境并使用 VSCODE 编程 安装前准备 安装 ESP8266 工具链 下载 ESP8266 SDK 编译 花絮 使用 Windo ...
- 2018-8-29-Roslyn-通过-Target-修改编译的文件
title author date CreateTime categories Roslyn 通过 Target 修改编译的文件 lindexi 2018-08-29 09:10:46 +0800 2 ...
- mysql锁机制之表锁(三)
顾名思义,表锁就是一锁锁一整张表,在表被锁定期间,其他事务不能对该表进行操作,必须等当前表的锁被释放后才能进行操作.表锁响应的是非索引字段,即全表扫描,全表扫描时锁定整张表,sql语句可以通过执行计划 ...
- 2018年DDoS攻击全态势:战胜第一波攻击成“抗D” 关键
2018年,阿里云安全团队监测到云上DDoS攻击发生近百万次,日均攻击2000余次.目前阿里云承载着中国40%网站,为全球上百万客户提供基础安全防御.可以说,阿里云上的攻防态势是整个中国攻防态势的缩影 ...
- poj1087&&hdu1526 最大流
多源多汇. 比较明显的建图.对于电器,可以从源点与各个电器相连,容量为1,表示这个电器有1个,然后对于各种接头,那可以各个接头与汇点相连,容量为1,表示每个接头只能用一次. 然后对于能够相互转换的接头 ...
- 深入理解spring注解之@ComponentScan注解
今天主要从以下几个方面来介绍一下@ComponentScan注解: @ComponentScan注解是什么 @ComponentScan注解的详细使用 1,@ComponentScan注解是什么 其实 ...
- thinkphp5.0验证码使用
如果没有安装验证码类,可在composer.json 文件的require里面添加 "topthink/think-captcha":"1.*",然后compo ...