Netty 文件下载例子

package bhz.netty.httpfile;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @author Administrator
 *访问路径:http://localhost:8765/sources/
 */
public class HttpFileServer {

    private static final String DEFAULT_URL = "/sources/";

    public void run(final int port, final String url) throws Exception {
    	EventLoopGroup bossGroup = new NioEventLoopGroup();
    	EventLoopGroup workerGroup = new NioEventLoopGroup();
		try {
		    ServerBootstrap b = new ServerBootstrap();
		    b.group(bossGroup, workerGroup)
			    .channel(NioServerSocketChannel.class)
			    .childHandler(new ChannelInitializer<SocketChannel>() {
				@Override
				protected void initChannel(SocketChannel ch)
					throws Exception {
					// 加入http的解码器
				    ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
				    // 加入ObjectAggregator解码器,作用是他会把多个消息转换为单一的FullHttpRequest或者FullHttpResponse
				    ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));
				    // 加入http的编码器
				    ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
				    // 加入chunked 主要作用是支持异步发送的码流(大文件传输),但不专用过多的内存,防止java内存溢出
				    ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
				    // 加入自定义处理文件服务器的业务逻辑handler
				    ch.pipeline().addLast("fileServerHandler",
					    new HttpFileServerHandler(url));
				}
			    });
		    ChannelFuture future = b.bind("127.0.0.1", port).sync();
		    System.out.println("HTTP文件目录服务器启动,网址是 : " + "http://localhost:"  + port + url);
		    future.channel().closeFuture().sync();
		} finally {
		    bossGroup.shutdownGracefully();
		    workerGroup.shutdownGracefully();
		}
    }

    public static void main(String[] args) throws Exception {
		int port = 8765;
		String url = DEFAULT_URL;
		new HttpFileServer().run(port, url);
    }
}

 

package bhz.netty.httpfile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

import bhz.utils.HttpCallerUtils;
import bhz.utils.HttpProxy;

public class Test {

	
	public static void main(String[] args) throws Exception{
		Map<String, String> params = new HashMap<String, String>();
		byte[] ret = HttpCallerUtils.getStream("http://127.0.0.1:8765/sources/a.doc", params);
		
		//byte[] ret = HttpProxy.get("http://192.168.1.111:8765/images/006.jpg");
        //写出文件
        String writePath = System.getProperty("user.dir") + File.separatorChar + "receive" +  File.separatorChar + "a.doc";
        FileOutputStream fos = new FileOutputStream(writePath);
        fos.write(ret);
        fos.close();    
		
		
	}
}

 

package bhz.netty.httpfile;

import static io.netty.handler.codec.http.HttpHeaderNames.*;
import static io.netty.handler.codec.http.HttpMethod.*;
import static io.netty.handler.codec.http.HttpResponseStatus.*;
import static io.netty.handler.codec.http.HttpVersion.*;
import static io.netty.handler.codec.http.HttpMethod.GET;
import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;
import static io.netty.handler.codec.http.HttpResponseStatus.FOUND;
import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;
import static io.netty.handler.codec.http.HttpResponseStatus.METHOD_NOT_ALLOWED;
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelProgressiveFuture;
import io.netty.channel.ChannelProgressiveFutureListener;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderUtil;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.stream.ChunkedFile;
import io.netty.util.CharsetUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.regex.Pattern;

import javax.activation.MimetypesFileTypeMap;

public class HttpFileServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    
	private final String url;

    public HttpFileServerHandler(String url) {
    	this.url = url;
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
    	//对请求的解码结果进行判断:
		if (!request.decoderResult().isSuccess()) {
			// 400
		    sendError(ctx, BAD_REQUEST);
		    return;
		}
		//对请求方式进行判断:如果不是get方式(如post方式)则返回异常
		if (request.method() != GET) {
			// 405
		    sendError(ctx, METHOD_NOT_ALLOWED);
		    return;
		}
		//获取请求uri路径
		final String uri = request.uri();
		//对url进行分析,返回本地系统
		final String path = sanitizeUri(uri);
		//如果 路径构造不合法,则path为null
		if (path == null) {
			//403
		    sendError(ctx, FORBIDDEN);
		    return;
		}
		// 创建file对象
		File file = new File(path);
		// 判断文件是否为隐藏或者不存在
		if (file.isHidden() || !file.exists()) {
			// 404 
		    sendError(ctx, NOT_FOUND);
		    return;
		}
		// 如果为文件夹
		if (file.isDirectory()) {
		    if (uri.endsWith("/")) {
		    	//如果以正常"/"结束 说明是访问的一个文件目录:则进行展示文件列表(web服务端则可以跳转一个Controller,遍历文件并跳转到一个页面)
		    	sendListing(ctx, file);
		    } else {
		    	//如果非"/"结束 则重定向,补全"/" 再次请求
		    	sendRedirect(ctx, uri + '/');
		    }
		    return;
		}
		// 如果所创建的file对象不是文件类型
		if (!file.isFile()) {
			// 403
		    sendError(ctx, FORBIDDEN);
		    return;
		}
		
		//随机文件读写类
		RandomAccessFile randomAccessFile = null;
		try {
		    randomAccessFile = new RandomAccessFile(file, "r");// 以只读的方式打开文件
		} catch (FileNotFoundException fnfe) {
			// 404
		    sendError(ctx, NOT_FOUND);
		    return;
		}
		
		//获取文件长度
		long fileLength = randomAccessFile.length();
		//建立响应对象
		HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
		//设置响应信息
		HttpHeaderUtil.setContentLength(response, fileLength);
		//设置响应头
		setContentTypeHeader(response, file);
		//如果一直保持连接则设置响应头信息为:HttpHeaders.Values.KEEP_ALIVE
		if (HttpHeaderUtil.isKeepAlive(request)) {
		    response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
		}
		//进行写出
		ctx.write(response);
		
		//构造发送文件线程,将文件写入到Chunked缓冲区中
		ChannelFuture sendFileFuture;
		//写出ChunkedFile
		sendFileFuture = ctx.write(new ChunkedFile(randomAccessFile, 0, fileLength, 8192), ctx.newProgressivePromise());
		//添加传输监听
		sendFileFuture.addListener(new ChannelProgressiveFutureListener() {
		    @Override
		    public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) {
				if (total < 0) { // total unknown
				    System.err.println("Transfer progress: " + progress);
				} else {
				    System.err.println("Transfer progress: " + progress + " / " + total);
				}
		    }
		    @Override
		    public void operationComplete(ChannelProgressiveFuture future) throws Exception {
		    	System.out.println("Transfer complete.");
		    }
		});
		
		//如果使用Chunked编码,最后则需要发送一个编码结束的看空消息体,进行标记,表示所有消息体已经成功发送完成。
		ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
		//如果当前连接请求非Keep-Alive ,最后一包消息发送完成后 服务器主动关闭连接
		if (!HttpHeaderUtil.isKeepAlive(request)) {
		    lastContentFuture.addListener(ChannelFutureListener.CLOSE);
		}
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    	//cause.printStackTrace();
		if (ctx.channel().isActive()) {
		    sendError(ctx, INTERNAL_SERVER_ERROR);
		    ctx.close();
		}
    }

    //非法URI正则
    private static final Pattern INSECURE_URI = Pattern.compile(".*[<>&\"].*");

    /**
     * <B>方法名称:</B>解析URI<BR>
     * <B>概要说明:</B>对URI进行分析<BR>
     * @param uri netty包装后的字符串对象
     * @return path 解析结果
     */
    private String sanitizeUri(String uri) {
		try {
			//使用UTF-8字符集
		    uri = URLDecoder.decode(uri, "UTF-8");
		} catch (UnsupportedEncodingException e) {
		    try {
		    	//尝试ISO-8859-1
		    	uri = URLDecoder.decode(uri, "ISO-8859-1");
		    } catch (UnsupportedEncodingException e1) {
		    	//抛出预想外异常信息
		    	throw new Error();
		    }
		}
		// 对uri进行细粒度判断:4步验证操作
		// step 1 基础验证
		if (!uri.startsWith(url)) {
		    return null;
		}
		// step 2 基础验证
		if (!uri.startsWith("/")) {
		    return null;
		}
		// step 3 将文件分隔符替换为本地操作系统的文件路径分隔符
		uri = uri.replace('/', File.separatorChar);
		// step 4 二次验证合法性
		if (uri.contains(File.separator + '.')
			|| uri.contains('.' + File.separator) || uri.startsWith(".")
			|| uri.endsWith(".") || INSECURE_URI.matcher(uri).matches()) {
		    return null;
		}
		//当前工程所在目录 + URI构造绝对路径进行返回 
		return System.getProperty("user.dir") + File.separator + uri;
    }
    
    //文件是否被允许访问下载验证
    private static final Pattern ALLOWED_FILE_NAME = Pattern.compile("[A-Za-z0-9][-_A-Za-z0-9\\.]*");

    private static void sendListing(ChannelHandlerContext ctx, File dir) {
    	// 设置响应对象
		FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK);
		// 响应头
		response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8");
		// 追加文本内容
		StringBuilder ret = new StringBuilder();
		String dirPath = dir.getPath();
		ret.append("<!DOCTYPE html>\r\n");
		ret.append("<html><head><title>");
		ret.append(dirPath);
		ret.append(" 目录:");
		ret.append("</title></head><body>\r\n");
		ret.append("<h3>");
		ret.append(dirPath).append(" 目录:");
		ret.append("</h3>\r\n");
		ret.append("<ul>");
		ret.append("<li>链接:<a href=\"../\">..</a></li>\r\n");
		
		// 遍历文件 添加超链接
		for (File f : dir.listFiles()) {
			//step 1: 跳过隐藏或不可读文件 
		    if (f.isHidden() || !f.canRead()) {
		    	continue;
		    }
		    String name = f.getName();
		    //step 2: 如果不被允许,则跳过此文件
		    if (!ALLOWED_FILE_NAME.matcher(name).matches()) {
		    	continue;
		    }
		    //拼接超链接即可
		    ret.append("<li>链接:<a href=\"");
		    ret.append(name);
		    ret.append("\">");
		    ret.append(name);
		    ret.append("</a></li>\r\n");
		}
		ret.append("</ul></body></html>\r\n");
		//构造结构,写入缓冲区
		ByteBuf buffer = Unpooled.copiedBuffer(ret, CharsetUtil.UTF_8);
		//进行写出操作
		response.content().writeBytes(buffer);
		//重置写出区域
		buffer.release();
		//使用ctx对象写出并且刷新到SocketChannel中去 并主动关闭连接(这里是指关闭处理发送数据的线程连接)
		ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    }

    //重定向操作
    private static void sendRedirect(ChannelHandlerContext ctx, String newUri) {
    	//建立响应对象
		FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, FOUND);
		//设置新的请求地址放入响应对象中去
		response.headers().set(LOCATION, newUri);
		//使用ctx对象写出并且刷新到SocketChannel中去 并主动关闭连接(这里是指关闭处理发送数据的线程连接)
		ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    }

    //错误信息
    private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
    	//建立响应对象
    	FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status.toString()+ "\r\n", CharsetUtil.UTF_8));
    	//设置响应头信息
    	response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
    	//使用ctx对象写出并且刷新到SocketChannel中去 并主动关闭连接(这里是指关闭处理发送数据的线程连接)
    	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    }

    private static void setContentTypeHeader(HttpResponse response, File file) {
    	//使用mime对象获取文件类型
    	MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
    	response.headers().set(CONTENT_TYPE, mimeTypesMap.getContentType(file.getPath()));
    }
}

 

好的,以下是一个简单的 Netty Bootstrap 学习例子,写在 main 方法中: ```java import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; 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.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; public class NettyClient { private final String host; private final int port; public NettyClient(String host, int port) { this.host = host; this.port = port; } public void run() throws InterruptedException { EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new NettyClientHandler()); } }); ChannelFuture future = bootstrap.connect(host, port).sync(); future.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } } public static void main(String[] args) throws InterruptedException { String host = "localhost"; int port = 8888; new NettyClient(host, port).run(); } } class NettyClientHandler extends ChannelInboundHandlerAdapter { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { String msg = "Hello, Netty!"; ByteBuf byteBuf = Unpooled.buffer(msg.getBytes().length); byteBuf.writeBytes(msg.getBytes()); ctx.writeAndFlush(byteBuf); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf byteBuf = (ByteBuf) msg; byte[] bytes = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(bytes); String result = new String(bytes); System.out.println("Received message from server: " + result); byteBuf.release(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } } ``` 这是一个简单的 Netty 客户端,它会连接到指定的服务器地址和端口,并向服务器发送一条消息,然后等待服务器的响应。在这个例子中,我们使用了 NioEventLoopGroup 和 NioSocketChannel,具体细节可以参考 Netty 官方文档。同时,我们也实现了一个 NettyClientHandler,它会在客户端与服务器建立连接后被调用,并且会在接收到服务器的响应后被调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值