wjc
2024-11-27 22c83310cbc4b04615be42d51645b47eff87cb7d
app/src/main/java/com/hdl/photovoltaic/internet/HttpServer/MyNanoHttpServer.java
@@ -25,7 +25,7 @@
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);
@@ -44,6 +44,7 @@
        }
        return myNanoHttpServer;
    }
    @Override
    public Response serve(IHTTPSession session) {
@@ -78,30 +79,37 @@
            //驱动升级文件路径
            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)");
                    }
                    //根据请求大小创建缓存区
@@ -111,7 +119,7 @@
                    //响应客户端
                    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;
                }
            }