Java 通过套接字发送多个文件。未知文件数

Java 通过套接字发送多个文件。未知文件数,java,file,sockets,Java,File,Sockets,我正在制作一个能够发送文件的通讯器。 到目前为止,我设法使用附加线程(侦听器)使文本发送工作正常 我试图对文件做同样的事情,但我不知道,我如何才能做一个文件侦听器——一个线程,它检测传入的文件,下载它并侦听另一个文件。另外,我不知道我是否正确地设置了文件发送器。你能帮忙吗 当前发件人代码: try { InputStream in = new FileInputStream(fileToSend); OutputStream out = fileConn.getOutputStr

我正在制作一个能够发送文件的通讯器。 到目前为止,我设法使用附加线程(侦听器)使文本发送工作正常

我试图对文件做同样的事情,但我不知道,我如何才能做一个文件侦听器——一个线程,它检测传入的文件,下载它并侦听另一个文件。另外,我不知道我是否正确地设置了文件发送器。你能帮忙吗

当前发件人代码:

try {
    InputStream in = new FileInputStream(fileToSend);
    OutputStream out = fileConn.getOutputStream();
    Controller.copyData(in, out);
    out.close();
    in.close();
} catch (IOException e) {
    System.out.println("Problem!");
}
和接收器代码:

while (true)
{
    try {
        InputStream in = socket.getInputStream();
        OutputStream out = new FileOutputStream("hi.txt"); //temporary
        Controller.copyData(in, out);
        out.close();
        in.close();
    } catch (IOException e) {
        System.out.println("Problem!");
    }
}
编辑:我忘了添加我的复制数据。这是:

public static void copyData(InputStream in, OutputStream out) throws IOException{
    byte[] buf = new byte[8192];
    int len = 0;
    while ((len = in.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
}

您只需在监听线程中添加选项,等待不同的消息/选项并做出相应的反应,就可以实现这一点。例如:

private class WaitingThread extends Thread {
    volatile boolean awaitsServer = false;
    DataInputStream dataInput = new DataInputStream(inputStream);
    public void run() {
        while (connected) {
            int message = 0;
                if (awaitsServer == true) {
                    if (dIn.available() ==0) {
                        view.setLog("waiting");
                    } else {
                        message = dIn.readInt();
                        switch (tempMessage) {
                        // TO DO ALL KIND OF COMMUNICATION 

                        case 1: 
                            int filesize = dataInput.readInt();
                            int bytesRead;
                            int currentTot = 0;
                            byte[] bytearray = new byte[filesize];
                            int len = dataInput.readInt();
                            FileOutputStream fos = new FileOutputStream(currentlySelectedFile);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                    bytesRead = dataInput.read(bytearray, 0, bytearray.length);

                    currentTot = bytesRead;
                    do {
                        bytesRead = dataInput.read(bytearray, currentTot,
                                (len - currentTot));
                        if (bytesRead >= 0)
                            currentTot += bytesRead;

                    } while (currentTot < len);
                    bos.write(bytearray, 0, currentTot);
                    bos.close();

                    }
                                case 2: //GET TEXT
                                case 3: //DO SOMETHING ELSE
}}}
私有类WaitingThread扩展线程{
易失性布尔等待服务器=false;
DataInputStream dataInput=新的DataInputStream(inputStream);
公开募捐{
同时(连接){
int消息=0;
if(waitsserver==true){
如果(dIn.available()==0){
view.setLog(“等待”);
}否则{
message=dIn.readInt();
开关(临时消息){
//做各种各样的交流
案例1:
int filesize=dataInput.readInt();
int字节读取;
int currentTot=0;
byte[]bytearray=新字节[文件大小];
int len=dataInput.readInt();
FileOutputStream fos=新的FileOutputStream(currentlySelectedFile);
BufferedOutputStream bos=新的BufferedOutputStream(fos);
bytesRead=dataInput.read(bytearray,0,bytearray.length);
currentTot=字节读取;
做{
bytesRead=dataInput.read(bytearray,currentTot,
(len-currentTot));
如果(字节读取>=0)
currentTot+=字节读取;
}while(currentTot

顺便说一句,你有如何发送文件的示例。

你在使用Java 7+?我几天前下载了JDK,我想是的。经典的毫无意义,浪费CPU的误用
available()
;典型滥用过大的缓冲区,可能无法放入内存,或者缓冲区大小可能无法放入
int
K,这会不必要地增加延迟。有关正确的解决方案,请参阅重复问题中的我的答案。