0

i am trying to develop an application to transfer large files over a network.Am trying to implement the same splitting files into chunks how to split a large file into chunks of 50kb each? And how to put it back to original on basis of hashcode( for error control)?

Nakul Narayanan
  • 1,412
  • 13
  • 17
  • This may be helpful http://stackoverflow.com/q/10864317/1393766 – Pshemo Jul 27 '15 at 17:06
  • 1
    Which transfer protocol are you planning to use for the chunks? If you go TCP you do not need error control, TCP does this for you. – alk Jul 27 '15 at 17:10

1 Answers1

0

i know this is not really what you asked for but you can transfer large files like this Sender :

            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();

hope this was a little helpful

L4B0MB4
  • 151
  • 1
  • 11
  • The OP was asking to cut the data into chunks sized apx. 50K, not 1 byte. – alk Jul 28 '15 at 17:57
  • You could at least have taken the burden to provide an example using this implemenation of `read()`: http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read%28byte[]%29 – alk Jul 28 '15 at 18:00
  • no offense but you could answer him instead of nagging about my post – L4B0MB4 Jul 28 '15 at 18:02
  • 2
    Fair enough, wasn't ment offensive either ... just didn't understand .. ;) – alk Jul 28 '15 at 18:21