简体   繁体   English

将大文件客户端发送到服务器

[英]Sending large files Client to Server

I'm trying to send files to my server. 我正在尝试将文件发送到服务器。 I got it working with files smaller than 100mb. 我可以处理小于100mb的文件。 Otherwise I ran out of heap. 否则我用光了堆。 So I remade it but can't really get it to work. 因此,我重新制作了它,但实际上无法使其正常工作。 I also got the to work except for reading the last data I get stuck in bis.read(buffer) because it didn't know when the file ended. 除了读取最后卡在bis.read(buffer)中的数据外,我还可以工作,因为它不知道文件何时结束。 So I tried to send the length of each segment so that the bufferedInputStream know when to stop reading. 因此,我尝试发送每个段的长度,以便bufferedInputStream知道何时停止读取。

Any idea what's wrong? 知道有什么问题吗?

Sender Code: 发件人代码:

        FileInputStream fis = new FileInputStream(file);
        byte[] buffer = new byte[2048];
        Integer bytesRead = 0;
        BufferedOutputStream bos = new BufferedOutputStream(clientSocket.getOutputStream());
        BufferedInputStream bis = new BufferedInputStream(fis);

        while ((bytesRead = bis.read(buffer)) > 0) {
            objectOutStream.writeObject(bytesRead);
            bos.write(buffer, 0, bytesRead);

        }
        System.out.println("Sucess sending file");

Receiver (Server): 接收方(服务器):

       fileName = request.getFileName();
       int size = (int) request.getSize();

        BufferedInputStream bis = new BufferedInputStream(clientSocket.getInputStream());

        FileOutputStream fos = new FileOutputStream(fileName);
        int totalBytesReceived = 0;
        int blockSize = 0;
        while (totalBytesReceived < size) {
            Object o = ois.readObject();

            if (!(o instanceof Integer)) {
                System.out.println("Something is wrong");
            }
            blockSize = (Integer) o;

            buffer = new byte[blockSize];

            bis.read(buffer);
            totalBytesReceived += blockSize;
            fos.write(buffer, 0, blockSize);
        }
        System.out.println("File succes");

Your reading code on the server side should look the same as on the client side. 服务器端的阅读代码应与客户端相同。

// copy from bis to bos, using a buffer.
for(int len; (len = bis.read(buffer)) > 0) {
    bos.write(buffer, 0, len);
}

On the client side you want 在客户端,您想要

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filename));
BufferedOutputStream bos = new BufferedOutputStream(clientSocket.getOutputStream());

on the server side you want. 在您想要的服务器端。

BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filename));

when finished you want to 完成后,您想

bos.close();
bis.close();

If you are going to use an ObjectOutputStream (and I suggest you don't) you need to use only this stream, not a mixture of streams. 如果要使用ObjectOutputStream(建议不要使用),则只需使用此流,而不必使用流的混合。

You can simplify your code. 您可以简化您的代码。 You will get -1 from bis.read() when you reach the end of the stream. 当您到达流的末尾时,将从bis.read()获得-1。 That way you don't need to know how long the file will be. 这样,您就无需知道文件将要存储多长时间。 And flush() and close() the streams on the server and client. 然后在服务器和客户端上flush()close()流。 As the other poster says, don't use ObjectOutputStream or ObjectIntputStream . 就像其他海报所说的那样,不要使用ObjectOutputStreamObjectIntputStream You'll get serialization headers which will appear to corrupt your data. 您将获得序列化头,这些头似乎会破坏您的数据。

   fileName = request.getFileName();

    BufferedInputStream bis = new BufferedInputStream(clientSocket.getInputStream());

    FileOutputStream fos = new FileOutputStream(fileName);
    int data = bis.read(); //reads an int
    while (data != -1) {
        fos.write(data);
        data = bis.read();

    }
    fos.flush();
    fos.close();

    System.out.println("File succes");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM