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

发布于 2024-11-07 02:33:55 字数 1792 浏览 5 评论 0原文

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

下面是一些代码:

传输文件的函数

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);
        }
    }

我必须提到 osDataOutputStreamcomunicare< /code> 是 Socket 类型。

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

I am currently trying to design a client-server application, something like this: the user connects to the server and when the authentication is OK the server send to the user some files. The problem is that those files are written in a single file (with my method).

Here is some code:

The function which transfers the file

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);
        }

    }

And the function that choose which file have to be send

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);
        }
    }

I have to mention that os is DataOutputStream and comunicare is Socket type.

I think that the problem is that I combine writeBytes with write. Can anyone help me with this problem? How can I make the server and the client to receive both files and messages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昵称有卵用 2024-11-14 02:33:55

我做了这样的事情:

Server -send Command
服务器发送文件
客户端 - 确认文件成功

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

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

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);
    }
}

I do something like this:

Server -send Command
Server -send File
Client -confirms file sucessfull

Server -send Command
Server -send File
Client -confirms file sucessfull
...

like this...supose you have a socket from a client, than you...

socket.getOutputStream.write("FILE: SomeFile.bin, SIZE: 872973493\r\n".getBytes("choose an encoding"))
socket.getOutputStream.flush(); (you will only need to flush if you are expecting a server string response like: OK SEND ME THE FILE, IM READY, otherwise no need too)
client reads and see that is a file and it has this bytes size, so it starts reading from socket.getInputStream untill it gets the length of file as expected.
after this clients confirm he recived the file

then server can send another file, you could do instead of FILE, use IMAGE: or anything you want. You just have to read the message from client side to see if it is a file or image

Here are some functions that might help you:

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文