0

如果文件大于 12MB 大于块发送的文件,我正在尝试在客户端-服务器之间传输文件,否则它会正常发送。我的主要问题是每次我传输的东西都会比原来的字节多,我需要在完成后使用摘要消息,所以它不会按原样工作,另一个是当我尝试发送文件时通过网络,客户端读取要发送的文件比服务器写入它的速度更快,因此客户端关闭程序关闭连接,从而损坏文件。我的转移代码如下:

这是客户端转移代码:

   if(fSize>maxfileSize){
        totbLidos = 0;
        byte[] fBytes = new byte[fBsize];
        while(totbLidos < fSize){
            int bRemain = (int) f.length() - totbLidos;
            if(bRemain < fBsize){
                fBsize = bRemain;
            }
            int bRead = tFile.read(fBytes, 0, fBsize);
            tServidor.write(fBytes, 0, fBsize);
            tServidor.flush();
            if(bRead>0){
                totbLidos+=bRead;
            }

            System.out.println("Total Bytes Lidos: " + totbLidos);

        }

        tFile.close();
        System.out.println("Ficheiro enviado");
        cliente.close();
    }
    else{
        totbLidos = 0;
        byte[] fBytes = new byte[fSize];
        while(totbLidos < fSize){
            int bRead = tFile.read(fBytes,0,fSize);
            if(bRead>0){
                totbLidos+=bRead;
            }
            tServidor.write(fBytes, 0, fSize);
            System.out.println("Total Bytes Lidos: " + totbLidos);
            tServidor.flush();
        }
        tFile.close();
        System.out.println("Ficheiro enviado");
        cliente.close();
    }
 }

服务器传输代码:

if(fSize > maxfileSize){
            totbLidos = 0;
            DataInputStream tFile = new DataInputStream(cliente.getInputStream());
            BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(fName));
            byte[] fBytes = new byte[fBsize];
            while(totbLidos < fSize){
                int bRemain = size - totbLidos;
                if(bRemain < fBsize){
                    fBsize = bRemain;
                }
                int bRead = tFile.read(fBytes, 0, fBsize);
                fos.write(fBytes);
                fos.flush();
                if(bRead>0){
                    totbLidos+=bRead;
                }
                System.out.println("Bytes lidos: " + bRead);
                System.out.println("Total Bytes Escritos: " + totbLidos);
            }
            System.out.println("Ficheiro recebido");

            fos.close();
            tFile.close();
            cliente.close();
            servidor.close();
        }
        else if(fSize < maxfileSize){
            totbLidos = 0;
            DataInputStream tFile = new DataInputStream(cliente.getInputStream());
            BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(fName));
            byte[] fBytes = new byte[fSize];
            while(totbLidos < fSize){
                int bRead = tFile.read(fBytes,0,fSize);
                fos.write(fBytes);
                fos.flush();
                if(bRead>0){
                    totbLidos+=bRead;
                }
                System.out.println("Total Bytes Escritos: " + totbLidos);
            }
            System.out.println("Ficheiro recebido");
            fos.close();
            tFile.close();
            cliente.close();
            servidor.close();
        }
}
4

2 回答 2

2

您写入的字节数与您在此处读取的字节数不同。

fos.write(fBytes);

尝试使用

fos.write(fBytes, 0, bRead);
于 2012-12-13T21:08:41.350 回答
0

In addition to @Peter Lawrey answer I suggest you to reuse Apache Commons IO API to avoid this kind of bugs in future:

IOUtils.copyLarge(InputStream input, OutputStream output, long inputOffset, long length)
于 2012-12-13T21:23:33.560 回答