通过Netty.io发送Jar文件

时间:2018-02-18 17:15:33

标签: java netty

我编写了一个Netty.io系统,我可以在其中轻松地来回发送文本文件,但现在我不仅希望在字节级别发送文本文件,而且如果我在此之前发布了一个jar,甚至在字节级发送一个jar文件。

现在我只尝试将它们像普通文件一样发送到逻辑上腐败的罐子里出来。

所以我发送文本文件:

    public void sendFile(Channel channel, File files, String path)  {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    sendFileInfo(channel,files,path);
                    while (file.containsKey(files.getName())) {
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    try {
                        FileInputStream fis = new FileInputStream(files);
                        byte[] bytes = new byte[512];
                        int i =0;
                        int length;
                        while((length =fis.read(bytes))>0){
                            PacketOutFile512Bytes bit = new PacketOutFile512Bytes(i,files.getName(),length,bytes);
                            channel.writeAndFlush(bit);
                            if(!cache.containsKey(files.getName()))
                                cache.put(files.getName(),new HashMap<>());
                            HashMap<Integer, Timer> timecache = cache.get(files.getName());
                            timecache.put(i,new Timer());
                            timecache.get(i++).schedule(new TimerTask() {
                                @Override
                                public void run() {
                                    channel.writeAndFlush(bit);
                                }
                            },Main.getInstance().getTimeout()*1000,Main.getInstance().getTimeout()*1000);
                            Thread.sleep(100);
                        }
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
}


public void writeFile(Channel channel, String file, int length){
        new Thread(new Runnable() {
            @Override
            public void run() {
                File sending = new File(path.get(file),file);
                try {
                    sending.createNewFile();
                    FileOutputStream fos = new FileOutputStream(file);
                    for(int i =0; i< amountofPackets.get(file);i++)
                        fos.write(cache.get(file).get(i),0,length);
                    fos.close();
                    channel.writeAndFlush(new PacketOutFileSucess(file));
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }).start();
    }

现在我正在寻找一种方法,如何在可以使用相同包发送的字节级别上复制jar。

这是数据包查找字节片段的方式:

import de.letsplaybar.fileserver.ValueSizeException;
import lombok.Getter;

public class PacketInFile512Bytes implements Packet {

    private @Getter int packetId;

    private @Getter String name;

    private @Getter int length;

    private @Getter byte[] value;


    public PacketInFile512Bytes() {
        value = new byte[512];
    }

    public PacketInFile512Bytes(int packetId, String name, int length, byte[] value) throws ValueSizeException {
        if(value.length != 512)
            throw new ValueSizeException();
        this.packetId = packetId;
        this.name = name;
        this.length = length;
        this.value = value;
    }

    @Override
    public void read(PacketSerilizer serilizer) {
        packetId = serilizer.readInt();
        name = serilizer.readString();
        length = serilizer.readInt();
        for (int i = 0; i<512;i++)
            value[i] = serilizer.readByte();
    }

    @Override
    public void write(PacketSerilizer serilizer) {
        serilizer.writeInt(packetId);
        serilizer.writeString(name);
        serilizer.writeInt(length);
        serilizer.writeBytes(value);
    }
}

谁知道如何做到这一点?

提前感谢你。

Letsplaybar

0 个答案:

没有答案