基于COAP 开源库Californium,实现的简单COAP 文件服务器DEMO

2 篇文章 0 订阅

基于COAP 开源库Californium,实现的简单COAP 文件服务器DEMO,新手上路,大神勿喷。

package com.quectel.coap.server.common;

import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.coap.CoAP;
import org.eclipse.californium.core.coap.CoAP.ResponseCode;
import org.eclipse.californium.core.coap.OptionSet;
import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.core.network.Exchange;
import org.eclipse.californium.core.server.MessageDeliverer;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.eclipse.californium.scandium.dtls.NULLClientKeyExchange;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Hello{

    public static void main(String[] args) {
        final CoapServer server = new CoapServer();//主机为localhost 端口为默认端口5683
        final MessageDeliverer messageDeliverer = new MessageDeliverer() {
            @Override
            public void deliverRequest(Exchange exchange) {
                System.out.println("get Request");
                if(exchange.getRequest().getCode() == CoAP.Code.GET) {
                    if(!exchange.getRequest().getOptions().hasBlock2())
                    {
                        Response response = new Response(ResponseCode.NOT_FOUND);
                        exchange.sendResponse(response);
                    }
                    String Path = "E://FOTA/" + exchange.getRequest().getOptions().getUriPathString();
                    File file = new File(Path);
                    if (file.exists()) {
                        if(file.canRead()) {
                                OptionSet optionSet = new OptionSet();
                                optionSet.setUriPath(exchange.getRequest().getOptions().getUriPathString());
                                int num = exchange.getRequest().getOptions().getBlock2().getNum();
                                int size = exchange.getRequest().getOptions().getBlock2().getSize();
                                Response response = null;
                                RandomAccessFile ran = null;
                                try{
                                    int hasread;
                                    ran=new RandomAccessFile (file,"r");
                                    ran.seek(num*size);
                                    byte[] b=new byte[size];
                                    if(ran.length() -num*size <= 0)
                                    {
                                        System.out.println("bad size");
                                        response = new  Response(ResponseCode.NOT_FOUND);
                                        exchange.sendResponse(response);
                                        return;
                                    }
                                    if (ran.length() > (num+1)*size) {
                                        optionSet.setBlock2(5, true, num);
                                    }
                                    else {
                                        optionSet.setBlock2(5, false, num);
                                    }
                                    hasread = ran.read(b);
                                    byte[] payload = new byte[hasread];
                                    System.arraycopy(b,0,payload,0,hasread);
                                    response = new Response(ResponseCode.CONTENT);
                                    response.setPayload(payload);

                                }catch (IOException e) {
                                    e.printStackTrace();
                                }finally {
                                    try {
                                        ran.close();
                                    }catch (IOException e)
                                    {
                                        e.printStackTrace();
                                    }
                                }
                                response.setOptions(optionSet);
                                exchange.sendResponse(response);
                            }
                        } else {
                        System.out.println("get file" + file.getPath().toString() + "Fail");
                        Response response = new Response(ResponseCode.NOT_FOUND);
                        exchange.sendResponse(response);
                        return;
                    }
                }
            }
            @Override
            public void deliverResponse(Exchange exchange, Response response) {
                exchange.sendResponse(response);
                System.out.println("send Request");
            }
        };
        server.setMessageDeliverer(messageDeliverer);
        server.start();
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lx121451

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值