Netty http服务端
文件读取服务
HttpFileServerHandler.class
public class HttpFileServerHandler extends SimpleChannelInboundHandler {
private static final Pattern INSECURE_URL = Pattern.compile(".*[<>&\"].*");
private static final Pattern ALLOWED_FILE_NAME = Pattern.compile("[A-Za-z0-9][-_A-Za-z0-9\\.]*");
private final String url;
public HttpFileServerHandler(String url) {
this.url = url;
}
private static void sendListing(ChannelHandlerContext ctx, File dir) {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=UTF-8");
StringBuilder buf = new StringBuilder();
String dirPath = dir.getPath();
buf.append("\r\n");
buf.append("");
buf.append(dirPath);
buf.append("目录:");
buf.append(" \r\n");
buf.append("");
buf.append(dirPath).append("目录:");
buf.append("\r\n");
buf.append("");
buf.append("- 链接:..
\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("- 链接:");
buf.append(name);
buf.append("
\r\n");
}
buf.append("