1

I have java code which purpose is download video files in small part (e.g. equal size of two part) using http request and later need to merge the files in the same condition so that I get back the original file. But I am getting the corrupted files while merging. In addition to it, the merge file size has increased than original.

File reading:

    public static int ReadFiles(String address,int StartPos,
    int LastPos,String filename) throws IOException
    {
     // Create the byte array to hold the data
      byte[] bytes = new byte[(int)LastPos];
    
    try {
        url = new URL(address);
        
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Opening connection to " + address + "...");
    System.out.println("Start Address " +  StartPos + " End Postion "+LastPos);
    URLConnection urlC = null;
    try {
        urlC = url.openConnection();
 

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    urlC.setRequestProperty("User-Agent","");
    urlC.connect();
    
    InputStream is = urlC.getInputStream();
    FileOutputStream fos = null;
    
    fos = new FileOutputStream(filename);
    int oneChar,CharRead;
    
    System.out.println("Downloading bytes: ");
    
    is.skip(StartPos);
    
    while (((oneChar = is.read()) != -1) ) {
           // System.out.print((char)oneChar);
            fos.write(oneChar);
            StartPos++;
            System.out.print("\r"+StartPos);
            if((StartPos>=LastPos))
            {
                System.out.println("Downloading break at : "+StartPos+"="+LastPos);
                break;
                
            }
        //    if(count==25260)break;
        }
    
    
    is.close();
    fos.close();
    //System.out.println(StartPos+ " byte(s) copied");
    System.out.println("File Downloader Completed Work!");

return 0;
}

Merging:

public static void mergeFiles(File[] files, File mergedFile) {
     
    FileWriter fstream = null;
    BufferedWriter out = null;
    try {
        fstream = new FileWriter(mergedFile, true);
         out = new BufferedWriter(fstream);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    for (File f : files) {
        System.out.println("merging: " + f.getName());
        FileInputStream fis;
        try {
            fis = new FileInputStream(f);
            BufferedReader in = new BufferedReader(new InputStreamReader(fis));

            String aLine;
            while ((aLine = in.readLine()) != null) {
                out.write(aLine);
                out.newLine();
            }

            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    try {
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
James Z
  • 12,209
  • 10
  • 24
  • 44
BINOD DEKA
  • 41
  • 7

1 Answers1

1

For partial download you can use Range request property (if server supports HTTP 1.1): Java: resume Download in URLConnection

Please don't use Reader during merging because it works with characters not bytes. You can use function like this instead:

public static void copyStream(InputStream is, OutputStream os) throws IOException {
    byte[] buffer = new byte[10000];
    while (true) {
        int len = is.read(buffer);
        if (len < 0)
            break;
        if (len > 0)
            os.write(buffer, 0, len);
    }
}

So for loop is going to be like:

for (File f : files) {
    System.out.println("merging: " + f.getName());
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(f);
        copyStream(fis, out);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fis != null)
            fis.close();
    }
}

PS: There are some standard implementations of stream copy operation as well. For instance: http://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/IOUtils.html#copy(java.io.InputStream,%20java.io.OutputStream)

Community
  • 1
  • 1
rsutormin
  • 1,629
  • 2
  • 17
  • 21