如何拆分大文件并在java

时间:2015-07-27 16:59:20

标签: java file sockets

我正在尝试开发一个通过网络传输大文件的应用程序。我试图将相同的拆分文件实现为块 如何将一个大文件分成每个50kb的块? 以及如何根据哈希码(用于错误控制)将其恢复原状?

1 个答案:

答案 0 :(得分:0)

我知道这不是你要求的,但你可以像这样传输大文件 发信人:

            try
            {
             ss = new ServerSocket(9244); // try to open ServerSocket
             ready =true;
            }
            catch(Exception e)
            {

            }
        Socket bs = ss.accept(); // reciever joins 
        OutputStream out = bs.getOutputStream();
        fis = new FileInputStream(f); // you open fileinputstream of the file you want to send 

        int x = 0;
        while (true) {
            x = fis.read();
            if (x == -1) { // until fis.read() doesn't return -1 wich means file is completly read write bytes out
                break;
            }
            out.write(x);
        }
        fis.close();
        out.close();
        bs.close();
        ss.close();

Reciever:

        try {
        Socket socket  = new Socket("127.0.0.1",9244); // connect to server
        InputStream in = socket.getInputStream();
        FileOutputStream fos = new FileOutputStream(f); 
        int x = 0;
        while (true) {
            x = in.read();
            if (x == -1) { // write data into file until in.read returns "-1"
                break;
            }
            fos.write(x);
        }
        in.close();
        fos.close();
        socket.close();

希望这有点帮助