java nio socket结合netty传输文件
来源:互联网 发布:公务员照片处理器软件 编辑:程序博客网 时间:2024/06/06 02:14
最近才开始研究java nio以及netty方面的知识,作为一个IT菜鸟,对这些东西几乎一无所知,所以有些地方的见解也纯属个人愚见,出现差错也在所难免
基于我的理解,java nio 之间数据的传递主要建立在channel和ByteBuffer之上,将要传递的数据转化成ByteBuffer写入到对应的channel中去,就实现了数据的写入
同理,使用netty实际上是基于java nio的一个框架,只不过对其进行了更好的封装,它通过管道中的Handler接收数据,然后将数据对应转化为对应的Bytebuf,实现数据的读取
以下就是一个文件传输例子的实现
其中客户端参考了网络上的数据重传以及给文件加上首部的一些方法(java nio 文件传输)
客户端代码:NioSocketClient.java
package com.test.nio.netty;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;import java.nio.channels.SocketChannel;import java.util.logging.Logger; public class NioSocketClient {private final static Logger log = Logger.getLogger(NioSocketClient.class.getName());private final static String START = "START";private InetSocketAddress inetSocketAddress; /* 发送数据缓冲区 */ private static ByteBuffer sendBuffer = ByteBuffer.allocate(1024); public CopyOfNioSocketClient(int port){ try{ inetSocketAddress = new InetSocketAddress("localhost", port); init(); } catch(Exception e){ e.printStackTrace(); } } private void init(){ try { SocketChannel socketChannel = SocketChannel.open(inetSocketAddress); socketChannel.configureBlocking(false); sendFile(socketChannel); } catch (Exception e) { e.printStackTrace(); } } private void sendFile(SocketChannel client) { FileInputStream fis = null; FileChannel channel = null; try { File file = new File("F:\\test\\1.avi"); fis = new FileInputStream(file); channel = fis.getChannel(); int i = 1; int sum = 0; int len = 0; //写文件头部 String file_name = file.getName(); Long file_length = file.length(); writeFileHead(client, START); writeFileHead(client, file_name); writeFileHead(client, String.valueOf(file_length)); //写文件内容 while((len = channel.read(sendBuffer)) != -1) { sendBuffer.flip(); int send = client.write(sendBuffer); log.info("已发送文件总字节数" + (sum += len)); log.info("i发送-->" + (i++) + " len:" + len + " send:" + send); // 考虑到服务器缓冲区满的情况 while(send == 0){ Thread.sleep(10); send = client.write(sendBuffer); log.info("i重新-->" + i + " len:" + len + " send:" + send); } sendBuffer.clear(); } } catch (Exception e) { e.printStackTrace(); } finally { try { channel.close(); fis.close(); client.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * @category 写文件头部 * @param channel * @param data * @throws IOException */ public void writeFileHead(SocketChannel channel, String data) throws IOException { byte[] d_content = data.getBytes(); byte[] d_len = i2b(d_content.length); sendBuffer.put(d_len); sendBuffer.put(d_content); } private byte[] i2b(int i) { //4个字节 return new byte[] { (byte) ((i >> 24) & 0xFF), (byte) ((i >> 16) & 0xFF), (byte) ((i >> 8) & 0xFF), (byte) (i & 0xFF), }; } public static void main(String[] args){ new NioSocketClient(12345); } }
服务器端:NettyServer.java
package com.test.nio.netty;import java.net.InetSocketAddress;import java.util.logging.Logger;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.Channel;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelOption;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioServerSocketChannel;public class NettyServer {private static final Logger log = Logger.getLogger(NettyServer.class.getName());private InetSocketAddress inetSocketAddress;public NettyServer(int port) {this.inetSocketAddress = new InetSocketAddress(port);}public void start() {EventLoopGroup bossGroup = new NioEventLoopGroup(1);EventLoopGroup workerGroup = new NioEventLoopGroup();try {ServerBootstrap b = new ServerBootstrap();b.group(bossGroup, workerGroup);b.channel(NioServerSocketChannel.class);b.option(ChannelOption.SO_KEEPALIVE, true);b.childHandler(new ChannelInitializer<Channel>() {@Overrideprotected void initChannel(Channel ch) throws Exception {//ch.pipeline().addLast(new StringEncoder());ch.pipeline().addLast(new FileReceiveHandler());}});log.info("server listen on " + inetSocketAddress.getPort() + ".");ChannelFuture f = b.bind(inetSocketAddress).sync();f.channel().closeFuture().sync();} catch (Exception e) {// TODO: handle exception} finally {bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}public static void main(String[] args) {NettyServer ns = new NettyServer(12345);ns.start();}}
Handler代码:FileReceivedHandler.java
package com.test.nio.netty;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInboundHandlerAdapter;import java.io.FileOutputStream;import java.util.logging.Logger;public class FileReceiveHandler extends ChannelInboundHandlerAdapter {private final static Logger log = Logger.getLogger(FileReceiveHandler.class.getName());private static String R_FLAG;//是否第一次接收数据private static String FILE_NAME = "DEFAULT";//默认文件名private static String STATE = "START";//文件传输标志private static Long FILE_LENGTH = 0L;//文件总长度private static Long READ_LENGTH = 0L;//文件已读长度private FileOutputStream fos;@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {writeMessageToClient(ctx, "connect success");log.info("--Client in--");R_FLAG = "FRIST";READ_LENGTH = 0L;}@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg)throws Exception {ByteBuf buf = (ByteBuf) msg;initialFileReceivedState(buf);if(isStart()) {saveFile(ctx, buf);} else {writeMessageToClient(ctx, "file hasn't been transported yet");}releaseBuf(buf);closeContext(ctx);}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)throws Exception {ctx.close();}/** * @category 向客户端写回文件传输进度 * @param ctx * @param transportLength */private void writeTransportProcess(ChannelHandlerContext ctx, Long transportLength) {long t_length = transportLength.longValue();long f_length = FILE_LENGTH.longValue();String process = "file transport percent ";String percent = String.format("%.1f", (t_length / (f_length * 1.0)) * 100) + "%";writeMessageToClient(ctx, process + percent);}/** * @category 向客户端写回信息 * @param ctx * @param msg */private void writeMessageToClient(ChannelHandlerContext ctx, String msg) {ctx.write(msg);}/** * @category 释放ByteBuf空间 * @param buf */private void releaseBuf(ByteBuf buf) {buf.clear();buf.release();}/** * @category 判断文件传输是否开始 * @return */private boolean isStart() {boolean flag = false;if("START".equalsIgnoreCase(STATE)) {flag = true;}return flag;}/** * @category 关闭Context * @return */private void closeContext(ChannelHandlerContext ctx) {if(FILE_LENGTH.longValue() == READ_LENGTH.longValue()) {ctx.close();}}/** * @category 文件存储 * @param buf * @throws Exception */private void saveFile(ChannelHandlerContext ctx, ByteBuf buf) throws Exception {if (buf.isReadable()) {READ_LENGTH += buf.readableBytes();//向客户端写回文件传输进度writeTransportProcess(ctx, READ_LENGTH);byte[] bytes = new byte[buf.readableBytes()];buf.readBytes(bytes);fos.write(bytes);fos.flush();}}/** * @category 将字节转化为整数 * @param b * @return */private int b2i(byte[] b) {int value = 0;for (int i = 0; i < 4; i++) {int shift = (4 - 1 - i) * 8;value += (b[i] & 0x000000FF) << shift;}return value;}/** * @category 读取头部信息 * @param in * @return */private String readFileHead(ByteBuf in) {byte[] stateBytes = new byte[4];in.readBytes(stateBytes);int stateLength = b2i(stateBytes);byte[] sendState = new byte[stateLength];in.readBytes(sendState);return new String(sendState);}/** * @category 初始化从客户端发过来的文件头部信息 * @param in * @throws Exception */private void initialFileReceivedState(ByteBuf in) throws Exception {if("FRIST".equalsIgnoreCase(R_FLAG)) {STATE = readFileHead(in);FILE_NAME = readFileHead(in);FILE_LENGTH = Long.valueOf(readFileHead(in));fos = new FileOutputStream("F:\\" + FILE_NAME);log.info("server: state->" + STATE);log.info("server: fileName->" + FILE_NAME);log.info("server: fileLength->" + FILE_LENGTH);R_FLAG = "OTHER";}}}
测试过程中,可能会出现"您的软件中途释放了一个连接"等类似错误,是因为在写入头部数据时出现差错,目前仍然在解决中...
0 0
- java nio socket结合netty传输文件
- Java nio 传输文件
- Java NIO传输文件
- java Socket传输文件
- java socket 传输文件
- Java NIO实例-ServerSocketChannel实现Socket传输
- Netty Java NIO 高性能Socket 开发框架
- Java Nio 实现文件的传输
- 利用java socket传输文件
- JAVA应用socket传输文件
- Java使用Socket传输文件
- java 利用socket传输文件
- java NIO: MINA, Netty
- Java NIO netty 简介
- java nio netty
- Java NIO 与 Netty
- netty(一) java NIO
- netty java nio总结
- STL算法目录
- ubuntu 64 位安装becompare
- java中时间比较
- 关于cocos2dx中的strcmp
- Fragment与Activity通讯
- java nio socket结合netty传输文件
- EA导出代码
- Android-ViewPager学习
- android mk文件常用宏整理
- mono for android 深入探究APK包结构
- Xcode查看函数的调用和被调用列表(类似VS的reference功能)
- http 状态码 504 502
- simhash, minhash ,lsh
- 动态规划 之 dp + 图搜索