Netty+SpringBoot写一个基于Http协议的文件服务器
本文参考《Netty权威指南》 NettyApplication
package com.xh.netty; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class NettyApplication { public static void main(String[] args) { SpringApplication.run(NettyApplication.class, args); String[] argList =args;
System.out.println("+++++++++++++Simple Netty HttpFileServer+++++++++++++++");
System.out.println("+ VERSION 1.0.1 +");
System.out.println("+ AUTHER:XH +");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++");
if (args.length==0){
System.out.println("Usage: java -var thisPackageName.jar [-options][args...]");
System.out.println("Use -h for more infomation");
System.out.println("default port is 8080 , webRoot is /root ");
}else {
for (int i=0;i<argList.length;i++){
if (argList[i].equalsIgnoreCase("-h")){
System.out.println("-p your Listern port");
System.out.println("-f your webRoot path");
System.out.println("Example:java -jar netty-0.0.1-SNAPSHOT.jar -p 80 -f /root");
return;
}else {
if (argList[i].equalsIgnoreCase("-p")){
try{
HttpFileServer.PORT=Integer.valueOf(argList[i+1]);
}catch (NumberFormatException e){
System.out.println("wrong number for you port");
System.out.println("Use -h for more infomation");
e.printStackTrace();
return;
}
}
if (argList[i].equalsIgnoreCase("-f")){
try{
HttpFileServer.WEBROOT=argList[i+1];
}catch (Exception e){
System.out.println("wrong path for you webRoot");
System.out.println("Use -h for more infomation");
e.printStackTrace();
return;
}
}
}
}
} try {
HttpFileServer.main();
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
HttpFileServer
package com.xh.netty; 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; /**
* Created by root on 8/14/17.
*/
public class HttpFileServer { public static String WEBROOT = "/root";
public static int PORT = 8080; public void run(final int port , final String url) throws InterruptedException {
EventLoopGroup bossGroup=new NioEventLoopGroup();
EventLoopGroup workerGroup=new NioEventLoopGroup();
try{
ServerBootstrap bootstrap=new ServerBootstrap();
bootstrap.group(bossGroup,workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast("http-decoder",new HttpRequestDecoder());
socketChannel.pipeline().addLast("http-aggregator",new HttpObjectAggregator(65536));
socketChannel.pipeline().addLast("http-encoder",new HttpResponseEncoder());
socketChannel.pipeline().addLast("http-chunked",new ChunkedWriteHandler());
socketChannel.pipeline().addLast("fileServerHandler",new HttpFileServerHandler(url)); }
});
ChannelFuture future = bootstrap.bind("127.0.0.1",port).sync();
System.out.println("服务器已启动>>网址:"+"127.0.0.1:"+port+url);
future.channel().closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
} } public static void main() throws InterruptedException { new HttpFileServer().run(PORT,WEBROOT);
}
}
HttpFileServerHandler
package com.xh.netty; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.http.HttpResponseStatus;
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.net.URLDecoder;
import java.util.regex.Pattern; import static io.netty.handler.codec.http.HttpHeaderNames.*;
import static io.netty.handler.codec.http.HttpHeaderUtil.isKeepAlive;
import static io.netty.handler.codec.http.HttpHeaderUtil.setContentLength;
import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE;
import static io.netty.handler.codec.http.HttpResponseStatus.*;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; /**
* Created by root on 8/14/17.
*/
public class HttpFileServerHandler extends SimpleChannelInboundHandler<FullHttpRequest>{ private final String url;
String WEBROOT = HttpFileServer.WEBROOT;
public HttpFileServerHandler(String url) {
this.url = url;
} protected void messageReceived(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest) throws Exception {
if (!fullHttpRequest.decoderResult().isSuccess()){
sendError(channelHandlerContext, HttpResponseStatus.BAD_REQUEST);
return;
} if (fullHttpRequest.method()!= HttpMethod.GET){
sendError(channelHandlerContext,HttpResponseStatus.METHOD_NOT_ALLOWED);
return;
} String uri=fullHttpRequest.uri();
if (uri==null||uri.trim().equalsIgnoreCase("")){
uri="/";
}
if (uri.trim().equalsIgnoreCase("/")){
uri= WEBROOT;
}
if(!uri.startsWith(WEBROOT)){
uri= WEBROOT +uri;
}
final String path=sanitizeUri(uri);
if (path==null){
sendError(channelHandlerContext,HttpResponseStatus.FORBIDDEN);
return;
} File file=new File(path);
if (file.isHidden()||!file.exists()){
sendError(channelHandlerContext,HttpResponseStatus.NOT_FOUND);
return;
} if (file.isDirectory()){
if (uri.endsWith("/")){
senfListing(channelHandlerContext,file); }else {
sendRedirect(channelHandlerContext,uri+"/");
} return;
} if (!file.isFile()){
sendError(channelHandlerContext,HttpResponseStatus.FORBIDDEN);
return;
} RandomAccessFile randomAccessFile=null;
try{
randomAccessFile=new RandomAccessFile(file,"r");
}catch (FileNotFoundException e){
e.printStackTrace();
sendError(channelHandlerContext,HttpResponseStatus.NOT_FOUND);
return;
} Long fileLength=randomAccessFile.length();
HttpResponse httpResponse=new DefaultHttpResponse(HTTP_1_1,OK);
setContentLength(httpResponse,fileLength);
setContentTypeHeader(httpResponse,file); if (isKeepAlive(fullHttpRequest)){
httpResponse.headers().set(CONNECTION,KEEP_ALIVE);
} channelHandlerContext.writeAndFlush(httpResponse);
ChannelFuture sendFileFuture = channelHandlerContext.write(
new ChunkedFile(randomAccessFile,0,fileLength,8192),channelHandlerContext.newProgressivePromise()); sendFileFuture.addListener(new ChannelProgressiveFutureListener() {
public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) {
if (total<0){
System.err.println("progress:"+progress);
}else {
System.err.println("progress:"+progress+"/"+total);
}
} public void operationComplete(ChannelProgressiveFuture future) {
System.err.println("complete");
}
}); ChannelFuture lastChannelFuture=channelHandlerContext.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
if (!isKeepAlive(fullHttpRequest)){
lastChannelFuture.addListener(ChannelFutureListener.CLOSE ); } } @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(".*[<>&\"].*"); public String sanitizeUri(String uri){
try{
uri= URLDecoder.decode(uri,"UTF-8");
}catch (Exception e){
try{
uri= URLDecoder.decode(uri,"ISO-8859-1");
}catch (Exception ew){
ew.printStackTrace();
}
} if (!uri.startsWith(url)){
return null; } if (!uri.startsWith("/")){
return null; } uri=uri.replace('/',File.separatorChar);
if (uri.contains(File.separator+'.')||uri.startsWith(".")||uri.endsWith(".")||INSECURE_URI.matcher(uri).matches()){
return null;
} return uri;//System.getProperty("user.dir")+uri } private static final Pattern ALLOWED_FILE_NAME=Pattern.compile("[a-zA-Z0-9\\.]*");
private void senfListing(ChannelHandlerContext channelHandlerContext, File dir) {
FullHttpResponse response=new DefaultFullHttpResponse(HTTP_1_1,OK);
response.headers().set(CONTENT_TYPE,"text/html;charset=UTF-8");
StringBuilder builder =new StringBuilder();
String dirPath=dir.getPath();
builder.append("<!DOCTYPE html> \r\n");
builder.append("<html><head><title>");
builder.append(dirPath);
builder.append("目录:");
builder.append("</title></head><body>\r\n");
builder.append("<h3>");
builder.append(dirPath).append("目录:");
builder.append("</h3>\r\n");
builder.append("<ul>");
builder.append("<li>链接:<a href=\"../\">..</a></li>\r\n");
for (File f:dir.listFiles()){
if (f.isHidden()||!f.canRead()){
continue;
}
String fname=f.getName();
if (!ALLOWED_FILE_NAME.matcher(fname).matches()){
continue;
}
builder.append("<li>链接:<a href=\" ");
builder.append(fname);
builder.append("\" >");
builder.append(fname);
builder.append("</a></li>\r\n");
}
builder.append("</ul></body></html>\r\n"); ByteBuf byteBuf= Unpooled.copiedBuffer(builder, CharsetUtil.UTF_8);
response.content().writeBytes(byteBuf);
byteBuf.release();
channelHandlerContext.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } private void sendRedirect(ChannelHandlerContext channelHandlerContext, String newUri) {
FullHttpResponse response=new DefaultFullHttpResponse(HTTP_1_1,FOUND);
response.headers().set(LOCATION,newUri);
channelHandlerContext.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
} private void sendError(ChannelHandlerContext channelHandlerContext, 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");
channelHandlerContext.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } private void setContentTypeHeader(HttpResponse httpResponse, File file) { MimetypesFileTypeMap mimetypesFileTypeMap=new MimetypesFileTypeMap();
httpResponse.headers().set(CONTENT_TYPE,mimetypesFileTypeMap.getContentType(file.getPath()));
} }
打包发布:
cd 到项目target同级目录
mvn clean package
然后 cd target/
java -jar netty-0.0.1-SNAPSHOT.jar -h运行
Netty+SpringBoot写一个基于Http协议的文件服务器的更多相关文章
- 教你如何使用Java手写一个基于链表的队列
在上一篇博客[教你如何使用Java手写一个基于数组的队列]中已经介绍了队列,以及Java语言中对队列的实现,对队列不是很了解的可以我上一篇文章.那么,现在就直接进入主题吧. 这篇博客主要讲解的是如何使 ...
- 使用springboot写一个简单的测试用例
使用springboot写一个简单的测试用例 目录结构 pom <?xml version="1.0" encoding="UTF-8"?> < ...
- 写一个基于TCP协议套接字,服务端实现接收客户端的连接并发
''' 写一个基于TCP协议套接字,服务端实现接收客户端的连接并发 ''' client import socket import time client = socket.socket() clie ...
- 闲来无事,写个基于UDP协议的Socket通讯Demo
项目一期已经做完,二期需求还没定稿,所以最近比较闲. 上一篇写的是TCP协议,今天写一下UDP协议.TCP是有连接协议,所以发送和接收消息前客户端和服务端需要建立连接:UDP是无连接协议,所以发送消息 ...
- [PHP]用PHP自己写一个基于zoomeye的api(偷懒必备quq)
0x01 起因 因为手速慢,漏洞刷不过别人,一个个手补确实慢,所以想自己写一个api,一键抓取zoomeye的20页,然后就可以打批量了 ovo(真是太妙了!) 0x02 动工 1.抓包做 ...
- 网络编程—【自己动手】用C语言写一个基于服务器和客户端(TCP)!
如果想要自己写一个服务器和客户端,我们需要掌握一定的网络编程技术,个人认为,网络编程中最关键的就是这个东西--socket(套接字). socket(套接字):简单来讲,socket就是用于描述IP地 ...
- 转:【专题十一】实现一个基于FTP协议的程序——文件上传下载器
引言: 在这个专题将为大家揭开下FTP这个协议的面纱,其实学习知识和生活中的例子都是很相通的,就拿这个专题来说,要了解FTP协议然后根据FTP协议实现一个文件下载器,就和和追MM是差不多的过程的,相信 ...
- 专题十一:实现一个基于FTP协议的程序——文件上传下载器
引言: 在这个专题将为大家揭开下FTP这个协议的面纱,其实学习知识和生活中的例子都是很相通的,就拿这个专题来说,要了解FTP协议然后根据FTP协议实现一个文件下载器,就和和追MM是差不多的过程的,相信 ...
- SpringBoot写一个登陆注册功能,和期间走的坑
文章目录 前言 1. 首先介绍项目的相关技术和工具: 2. 首先创建项目 3. 项目的结构 3.1实体类: 3.2 Mapper.xml 3.3 mapper.inteface 3.4 Service ...
随机推荐
- 日志收集-Elk6
一:前言 ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是开源软件.新增了一个FileBeat,它是一个轻量级的日志收集处理工具(A ...
- day9 字符串格式化输出 % .format()
常用的格式化输出方式1 % 方式 print("i am %s my hobby is %s" %("yt","eat")) 打印浮点数,. ...
- MT【45】抛物线外一点作抛物线的切线(尺规作图题)
注1:S为抛物线焦点 注2:由切线的唯一性,以及切线时可以利用MT[42]评得到三角形全等从而得到切线平分$\angle MQS$得到
- bat 脚本处理windows 文件
背景:以下脚本使用了导出文件列表.移动文件.复制文件.report 系统信息.分段执行的功能 主要针对在从事于Easeware公司中,对软件Bug中,所需文件的提取. 代码片段说明: cls ver ...
- TensorFlow分布式计算机制解读:以数据并行为重
Tensorflow 是一个为数值计算(最常见的是训练神经网络)设计的流行开源库.在这个框架中,计算流程通过数据流程图(data flow graph)设计,这为更改操作结构与安置提供了很大灵活性.T ...
- wireshark配合jmeter测试webservice接口
1.首先,获取本地和接口的ip,以便设置过滤 2.wireshark设置过滤 ip.dst==192.168.0.101 and ip.src==61.147.124.120 and http 3.执 ...
- WCF快速搭建Demo
WCF快速搭建Demo ps:本Demo只是演示如何快速建立WCF 1.首先完成IBLL.BLL.Model层的搭建,由于数据访问层不是重点,WCF搭建才是主要内容,所以本Demo略去数据访问层. 新 ...
- CF401D Roman and Numbers
题意: 将n(n<=10^18)的各位数字重新排列(不允许有前导零) 求 可以构造几个mod m等于0的数字 分析: 状态压缩 状态: 设f[s][k]表示对于选择数字组合的s来说,%m等于k的 ...
- (转)Java transient关键字使用小记
背景:最近在看java底层的源码实现,看到一个关键字,不是很熟悉,专门做个记录. 原文出处:http://www.importnew.com/21517.html#comment-637072 哎,虽 ...
- JSP总结(二)—Cookie(汇总)
注:后缀为汇总的基本上是整理一些网上的. 1. 什么是Cookie Cookie是Web服务器保存在用户硬盘上的一段文本.Cookie允许一个Web站点在用户电脑上保存信息并且随后再取回它 ...