java - 客户端-服务器应用程序文件传输

标签 java sockets client-server dataoutputstream

我目前正在尝试设计一个客户端服务器应用程序,如下所示:用户连接到服务器,当身份验证正常时,服务器向用户发送一些文件。问题是这些文件被写入单个文件中(使用我的方法)。

这是一些代码:

传输文件的函数

public void processFile(int id, DataOutputStream oStream, Socket socket, int tip){
        String fileName;
        if(tip==0){
            fileName="File"+Integer.toString(Intrebari[id])+".txt";
        }else{
            fileName="Image"+Integer.toString(Intrebari[id])+".jpg";
        }
        byte[] buffer=null;
        int bytesRead = 0;
        FileInputStream file=null;

        try {
            file = new FileInputStream(fileName);
            buffer = new byte[socket.getSendBufferSize()];
            while((bytesRead = file.read(buffer))>0)
                {
                oStream.write(buffer,0,bytesRead);
                }
            file.close();
        } catch (IOException ex) {
           System.out.println(ex);
        }

    }

以及选择要发送哪个文件的功能

private void send(int id) throws IOException {
        os.writeBytes("sendFile" + id+"\n");

        System.out.println("sendFile" + id);
        generatedFiles.processFile(id, os, comunicare, 0);
        if (generatedFiles.Imagini[id] == 1) {
            os.writeBytes("sendImage" + id+"\n");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(clientThread.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("sendImage" + id);
            generatedFiles.processFile(id, os, comunicare, 1);
        }
    }

我不得不提一下,osDataOutputStreamcomunicareSocket类型。

我认为问题在于我将 writeByteswrite 结合起来。谁能帮我解决这个问题吗?如何让服务器和客户端同时接收文件和消息?

最佳答案

我做了这样的事情:

服务器-发送命令 服务器发送文件 客户端-确认文件成功

服务器-发送命令 服务器发送文件 客户端-确认文件成功 ...

像这样...假设您有来自客户端的套接字,那么...

socket.getOutputStream.write("FILE: SomeFile.bin, SIZE: 872973493\r\n".getBytes("选择编码")) socket.getOutputStream.flush(); (仅当您期望服务器字符串响应时才需要刷新:OK SEND ME THE FILE, IM READY,否则也不需要) 客户端读取并看到这是一个文件并且它具有此字节大小,因此它开始从 socket.getInputStream 读取,直到它按预期获得文件的长度。 客户确认收到文件后

然后服务器可以发送另一个文件,您可以代替 FILE,使用 IMAGE: 或您想要的任何内容。您只需从客户端读取消息即可查看它是文件还是图像

以下是一些可能对您有帮助的功能:

public static void readInputStreamToFile(InputStream is, FileOutputStream fout,
        long size, int bufferSize) throws Exception
{
    byte[] buffer = new byte[bufferSize];
    long curRead = 0;
    long totalRead = 0;
    long sizeToRead = size;
    while(totalRead < sizeToRead)
    {
        if(totalRead + buffer.length <= sizeToRead)
        {
            curRead = is.read(buffer);
        }
        else
        {
            curRead = is.read(buffer, 0, (int)(sizeToRead - totalRead));
        }
        totalRead = totalRead + curRead;
        fout.write(buffer, 0, (int) curRead);
    }
}


public static void writeFileInputStreamToOutputStream(FileInputStream in, OutputStream out, int bufferSize) throws Exception
{
    byte[] buffer = new byte[bufferSize];
    int count = 0;
    while((count = in.read(buffer)) != -1)
    {
        out.write(buffer, 0, count);
    }
}

关于java - 客户端-服务器应用程序文件传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5967697/

相关文章:

java - 如何使用 firebase (FCM) 生成 token 并共享

java - 向现有类动态添加 getter 和 setter

python - 检查python套接字中的数据是否可用

c - 如何使用 C 中的套接字在不同计算机上运行客户端-服务器

delphi - TServerSocket : Confusion with Socket Objects

java - HTTPClient-java.net.SocketException : Unconnected sockets not implemented thrown

发送包含超过一定大小的字节数据的对象时出现Java StreamCorruptedException

路由器后面的 Python 客户端/服务器

Java客户端-服务器聊天应用程序: Weird problems with communication

java - 是否可以在单个端口上运行两个 NIO 服务器套接字?