| | |
| | | public class MyNanoHttpServer extends NanoHTTPD { |
| | | |
| | | //声明服务端 端口 |
| | | public static final Integer HTTP_PORT = 49152; |
| | | public static Integer HTTP_PORT = 49152;// 46219; |
| | | |
| | | public MyNanoHttpServer(String hostname, int port) { |
| | | super(hostname, port); |
| | |
| | | } |
| | | return myNanoHttpServer; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public Response serve(IHTTPSession session) { |
| | |
| | | //驱动升级文件路径 |
| | | String fileName = ((HTTPSession) session).getUri(); |
| | | if (!TextUtils.isEmpty(fileName)) { |
| | | if (fileName.contains(HdlFileLogic.getInstance().getDriveRootPath())) { |
| | | if (fileName.contains(HdlFileLogic.getInstance().getDriveRootPath()) |
| | | || fileName.contains(HdlFileLogic.getInstance().getFirmwareRootPath())) { |
| | | String range = session.getHeaders().get("range"); |
| | | if (TextUtils.isEmpty(range)) { |
| | | return newFixedLengthResponse("错误,头部没有range字段(-500)"); |
| | | } |
| | | if (range.contains("=")) { |
| | | HdlLogLogic.print("http逆变器请求===请求大小-->" + range, true); |
| | | if (!range.contains("=")) { |
| | | HdlLogLogic.print("错误,range的value格式不对.(-501)", true); |
| | | return newFixedLengthResponse("错误,range的value格式不对.(-501)"); |
| | | } |
| | | String[] bytesAry = range.split("="); |
| | | if (bytesAry.length < 2) { |
| | | HdlLogLogic.print("错误,range的value格式不对.(-502)", true); |
| | | return newFixedLengthResponse("错误,range的value格式不对.(-502)"); |
| | | } |
| | | String[] dataIndex = bytesAry[1].split("-"); |
| | | if (dataIndex.length < 2) { |
| | | HdlLogLogic.print("错误,range的value格式不对.(-503)", true); |
| | | return newFixedLengthResponse("错误,range的value格式不对.(-503)"); |
| | | } |
| | | int startIndex = Integer.parseInt(dataIndex[0]); |
| | | int endIndex = Integer.parseInt(dataIndex[1]); |
| | | if (endIndex + 1 - startIndex <= 0) { |
| | | HdlLogLogic.print("错误,请求大小有问题.(-504)", true); |
| | | return newFixedLengthResponse("错误,请求大小有问题.(-504)"); |
| | | } |
| | | //读取本地文件字节流 |
| | | byte[] sourceDataByte = HdlFileLogic.getInstance().readFileByte(fileName); |
| | | if (sourceDataByte == null || sourceDataByte.length == 0) { |
| | | HdlLogLogic.print("错误,本地找不到文件,请下载再试.(-505)", true); |
| | | return newFixedLengthResponse("错误,本地找不到文件,请下载再试.(-505)"); |
| | | } |
| | | //根据请求大小创建缓存区 |
| | |
| | | //响应客户端 |
| | | Response response = newFixedLengthResponse(newByte); |
| | | response.addHeader("Content-Range", range + "/" + sourceDataByte.length);//伟南需要这个字段 |
| | | HdlLogLogic.print("http回复数据===请求大小-->" + range + "=====文件大小-->" + sourceDataByte.length, true); |
| | | HdlLogLogic.print("http回复数据===请求大小--" + range + "=====文件大小--" + sourceDataByte.length, true); |
| | | return response; |
| | | } |
| | | } |