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

时间:2011-05-11 16:40:00

标签: 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合并。任何人都可以帮我解决这个问题吗?如何让服务器和客户端接收文件和消息?

1 个答案:

答案 0 :(得分:1)

我这样做:

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

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

像这样......假设你有一个来自客户的套接字,而不是你......

socket.getOutputStream.write(“FILE:SomeFile.bin,SIZE:872973493 \ r \ n”.getBytes(“选择编码”)) socket.getOutputStream.flush(); (如果你期望服务器字符串响应如下,你只需要刷新:OK发送我的文件,IM准备好,否则不需要) 客户端读取并看到它是一个文件并且它具有这个字节大小,因此它开始从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);
    }
}
相关问题