Netty笔记7----HTTP文件目录服务
-
启动类
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;
public class HttpFileServer {
// 这里目录要写完整的相对路径,包括src/main/java
private static final String DEFAULT_URL = "/src/main/java/";
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 {
ch.pipeline().addLast(new HttpRequestDecoder());
/**
* 将多个单一的消息转换为FullHttpRequest或者FullHttpResponse对象
* http解码器会在每个http消息中产生多个消息对象
*/
ch.pipeline().addLast(new HttpObjectAggregator(65536));
/**
*对http响应消息进行编码
*/
ch.pipeline().addLast(new HttpResponseEncoder());
/**
* 主要作用是支持异步发送大的码流(例如大文件传输)
* 但是不占用过多的内存,防止发生java内存溢出错误
*/
ch.pipeline().addLast(new ChunkedWriteHandler());
// HttpFileServerHandler用于文件服务器的业务逻辑处理
ch.pipeline().addLast( new HttpFileServerHandler(url));
}
});
String host = "127.0.0.1";
ChannelFuture future = b.bind(host, port).sync();
System.out.println("HTTP文件目录服务器启动,网址是 : http://" + host +":" + port + url);
future.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
int port = 9090;
String url = DEFAULT_URL;
new HttpFileServer().run(port, url);
}
}
-
实现类
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.handler.codec.http.*;
import io.netty.handler.stream.ChunkedFile;
import io.netty.util.CharsetUtil;
import javax.activation.MimetypesFileTypeMap;
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 static io.netty.handler.codec.http.HttpHeaders.Names.*;
import static io.netty.handler.codec.http.HttpMethod.GET;
import static io.netty.handler.codec.http.HttpResponseStatus.*;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
public class HttpFileServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
private final String url;
public HttpFileServerHandler(String url) {
this.url = url;
}
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
System.out.println("服务器接受消息:" + request.toString());
// 首先对HTTP请求的解码结果进行判断,如果解码失败,直接构造HTTP 400错误返回。
if (!request.decoderResult().isSuccess()) {
sendError(ctx, BAD_REQUEST);
return;
}
// 请求方法:如果不是表单设置为get请求,构造http 405错误返回
if (request.method() != GET) {
sendError(ctx, METHOD_NOT_ALLOWED);
return;
}
// 对请求的的URL进行包装
final String uri = request.uri();
// 展开URL分析
final String path = sanitizeUri(uri);
if (path == null) {
sendError(ctx, FORBIDDEN);
return;
}
File file = new File(path);
// 如果文件不存在或者是系统隐藏文件,则构造404 异常返回
if (file.isHidden() || !file.exists()) {
sendError(ctx, NOT_FOUND);
return;
}
// 如果文件是目录,则发送目录的连接给客户端浏览器
if (file.isDirectory()) {
if (uri.endsWith("/")) {
sendListing(ctx, file);
} else {
sendRedirect(ctx, uri + '/');
}
return;
}
// 用户在浏览器上点击超链接直接打开或者下载文件,合法性监测
if (!file.isFile()) {
sendError(ctx, FORBIDDEN);
return;
}
/**
* IE下才会打开文件,其他浏览器都是直接下载
* 随机文件读写类以读的方式打开文件
*/
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file, "r");// 以只读的方式打开文件
} catch (FileNotFoundException fnfe) {
sendError(ctx, NOT_FOUND);
return;
}
// 获取文件长度,构建成功的http应答消息
long fileLength = randomAccessFile.length();
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.headers().set(CONTENT_LENGTH, fileLength);
setContentTypeHeader(response, file);
if (HttpUtil.isKeepAlive(request)) {
response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
ctx.write(response);
/**
* 通过netty的ChunkedFile对象直接将文件写入到发送缓冲区,
* 最后为sendFileFeature增加GenericFeatureListener,
* 如果发送完成,打印“Transfer complete
*/
ChannelFuture sendFileFuture = ctx.write(new ChunkedFile(randomAccessFile, 0,
fileLength, 8192), ctx.newProgressivePromise());
sendFileFuture.addListener(new ChannelProgressiveFutureListener() {
public void operationProgressed(ChannelProgressiveFuture future,
long progress, long total) {
if (total < 0) {
System.err.println("Transfer progress: " + progress);
} else {
System.err.println("Transfer progress: " + progress + " / "
+ total);
}
}
public void operationComplete(ChannelProgressiveFuture future)
throws Exception {
System.out.println("Transfer complete.");
}
});
ChannelFuture lastContentFuture = ctx
.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
if (HttpUtil.isKeepAlive(request)) {
lastContentFuture.addListener(ChannelFutureListener.CLOSE);
}
}
/**
* DefaultFullHttpRequest, decodeResult: success)
* Accept: text/html, application/xhtml+xml, image/jxr
* Referer: http://127.0.0.1:9090/src/main/java/com/xdja/
* Accept-Language: zh-CN
* User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
* Accept-Encoding: gzip, deflate
* Host: 127.0.0.1:9090
* Connection: Keep-Alive
* Content-Length: 0
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
cause.printStackTrace();
if (ctx.channel().isActive()) {
sendError(ctx, INTERNAL_SERVER_ERROR);
}
}
private static final Pattern INSECURE_URI = Pattern.compile(".*[<>&\"].*");
private String sanitizeUri(String uri) {
//uri解码
try {
uri = URLDecoder.decode(uri, "UTF-8");
} catch (UnsupportedEncodingException e) {
try {
uri = URLDecoder.decode(uri, "ISO-8859-1");
} catch (UnsupportedEncodingException e1) {
throw new Error();
}
}
// URL合法性校验
if (!uri.startsWith(url)) {
return null;
}
// 替换uri中的"/"
uri = uri.replace('/', File.separatorChar);
// 合法性二次校验
if (uri.contains(File.separator + '.')
|| uri.contains('.' + File.separator)
|| uri.startsWith(".")
|| uri.endsWith(".")
|| INSECURE_URI.matcher(uri).matches()) {
return null;
}
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\\.]*");
/**
* 这里是构建了一个html页面返回给浏览器
*
* @param ctx
* @param dir
*/
/** <!DOCTYPE html>
* <html><head>
* <title>C:\Users\admin\IdeaProjects\nettyTest\src\main\java\com\xdja\domain 目录:</title>
* </head>
* <body>
* <h3>C:\Users\admin\IdeaProjects\nettyTest\src\main\java\com\xdja\domain 目录:</h3>
* <ul><li>链接:<a href="../">..</a></li>
* <li>链接:<a href="SubscribeReq.java">SubscribeReq.java</a></li>
* <li>链接:<a href="SubscribeResp.java">SubscribeResp.java</a></li>
* </ul>
* </body>
* </html>
*/
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 buf = new StringBuilder();
String dirPath = dir.getPath();
buf.append("<!DOCTYPE html>\r\n");
buf.append("<html><head><title>");
buf.append(dirPath);
buf.append(" 目录:");
buf.append("</title></head><body>\r\n");
buf.append("<h3>");
buf.append(dirPath).append(" 目录:");
buf.append("</h3>\r\n");
buf.append("<ul>");
// 此处打印了一个 .. 的链接
buf.append("<li>链接:<a href=\"../\">..</a></li>\r\n");
// 用于展示根目录下的所有文件和文件夹,同时使用超链接标识
for (File f : dir.listFiles()) {
if (f.isHidden() || !f.canRead()) {
continue;
}
String name = f.getName();
if (!ALLOWED_FILE_NAME.matcher(name).matches()) {
continue;
}
buf.append("<li>链接:<a href=\"");
buf.append(name);
buf.append("\">");
buf.append(name);
buf.append("</a></li>\r\n");
}
buf.append("</ul></body></html>\r\n");
System.out.println(buf);
ByteBuf buffer = Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8);
response.content().writeBytes(buffer);
buffer.release();
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.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.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
private static void setContentTypeHeader(HttpResponse response, File file) {
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
response.headers().set(CONTENT_TYPE,
mimeTypesMap.getContentType(file.getPath()));
}
}