java Netty 之 文件传输
/** * 文件传输接收端,没有处理文件发送结束关闭流的情景 */ public class FileServerHandler extends SimpleChannelHandler { private File file = new File("F:/2.txt"); private FileOutputStream fos; public FileServerHandler() { try { if (!file.exists()) { file.createNewFile(); } else { file.delete(); file.createNewFile(); } fos = new FileOutputStream(file); } catch (IOException e) { e.printStackTrace(); } } @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { ChannelBuffer buffer = (ChannelBuffer) e.getMessage(); int length = buffer.readableBytes(); buffer.readBytes(fos, length); fos.flush(); buffer.clear(); } }
/** * 文件发送客户端,通过字节流来发送文件,仅实现文件传输部分, * 没有对文件传输结束进行处理 * 应该发送文件发送结束标识,供接受端关闭流。 */ public class FileClientHandler extends SimpleChannelHandler { // 每次处理的字节数 private int readLength = 8; @Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { // 发送文件 sendFile(e.getChannel()); } private void sendFile(Channel channel) throws IOException { File file = new File("E:/1.txt"); FileInputStream fis = new FileInputStream(file); int count = 0; for (;;) { BufferedInputStream bis = new BufferedInputStream(fis); byte[] bytes = new byte[readLength]; int readNum = bis.read(bytes, 0, readLength); if (readNum == -1) { return; } sendToServer(bytes, channel, readNum); System.out.println("Send count: " + ++count); } } private void sendToServer(byte[] bytes, Channel channel, int length) throws IOException { ChannelBuffer buffer = ChannelBuffers.copiedBuffer(bytes, 0, length); channel.write(buffer); } }
这只是一个简单的文件传输的例子,可以做为样例借鉴。对于大文件传输的情景,本样例并不支持,会出现内存溢出的情景。