简体   繁体   English

通过套接字接收文件时的数据重复

[英]Duplication of data when receiving a file through a socket

Sorry for the bad English, I used Google translate. 对不起,英语不好,我用Google翻译。

Working on an Application that needs to transfer a file over the network with the ability to resume, the program works, but why when you open a text file transmitted through nano seen that data is duplicated. 在需要通过网络传输文件且具有恢复能力的应用程序上工作时,该程序可以运行,但是为什么当您打开通过nano传输的文本文件时却看到数据重复。

Server 服务器

OutputStream outToClient = socket.getOutputStream();

        File myfile = new File(filePath);
        System.out.println("файл " + myfile.toString());
        byte[] mybytearray = new byte[(int) myfile.length()];
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myfile));

        BufferedOutputStream bost = new BufferedOutputStream(new FileOutputStream(new File(filePath+"111")));
        bost.write(mybytearray, 0, mybytearray.length);
        bis.skip(filePosition);
        bis.read(mybytearray, 0, mybytearray.length);
        bost.write(mybytearray, 0, mybytearray.length);
        outToClient.write(mybytearray, 0, mybytearray.length);


        outToClient.flush();
        bis.close();

Client 客户

 byte[] mybytearray = new byte[1024];
    fos = new FileOutputStream(fullPath, true);
    bos = new BufferedOutputStream(fos);
    InputStream is = socket.getInputStream();
    int bytesRead = is.read(mybytearray, 0, mybytearray.length);

    bos.write(mybytearray, 0, bytesRead);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    do {
        baos.write(mybytearray);
        bytesRead = is.read(mybytearray);

        if (bytesRead != -1)
            filePosition += bytesRead;
    } while (bytesRead != -1);

    bos.write(baos.toByteArray());
    bos.close();   socket.close();

    is.close();
    socket.close();

You are getting some duplicate data because you are writing the same array contents to the output file twice before filling the array with new data. 之所以会得到一些重复的数据,是因为在将新数据填充数组之前,两次将相同的数组内容写入输出文件。 You are not using the streams very well. 您没有很好地使用流。 Try something more like this instead: 尝试类似这样的方法:

Server 服务器

OutputStream outToClient = socket.getOutputStream();

byte[] mybytearray = new byte[1024];
File myfile = new File(filePath);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myfile));
bis.skip(filePosition);
do {
    int bytesRead = bis.read(mybytearray, 0, mybytearray.length);
    if (bytesRead <= 0) {
        break;
    }
    outToClient.write(mybytearray, 0, bytesRead);
}
white (true);

outToClient.flush();
bis.close();

Client 客户

byte[] mybytearray = new byte[1024];
//todo: seek the output file to the starting filePosition
//instead of just appending to the end of the file...
fos = new FileOutputStream(fullPath, true);
bos = new BufferedOutputStream(fos);
InputStream is = socket.getInputStream();
do {
    int bytesRead = is.read(mybytearray, 0, mybytearray.length);
    if (bytesRead <= 0) {
        break;
    }
    bos.write(mybytearray, 0, bytesRead);
    filePosition += bytesRead;
} while (true);

bos.close();
is.close();
socket.close();

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

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