0

I have created a java program that downloads a file from a URL part by part into several files, then reads the bytes from those files into the full downloaded object. It works by separating sections of the file to be downloaded into threads. Every time my program downloads a file it gets all of the bytes and the file size is correct, but sometimes with an image the picture is distorted. Other times the image is perfect. What would cause this?

code that individual threads use to download file parts:

       URL xyz = new URL(urlStr);
       URLConnection connection= xyz.openConnection();
       // set the download range 
       connection.setRequestProperty("Range", "bytes="+fileOffset+"-");
       connection.setDoInput(true);
       connection.setDoOutput(true);
       // set input stream and output stream
       in = new BufferedInputStream(connection.getInputStream());
       fos = new FileOutputStream("part_"+this.partNumber);
       out = new  BufferedOutputStream(fos, this.downloadFileSize);
       // create buffer to read bytes from file into
       byte[] contentBytes = new byte[downloadFileSize];
       // read contents into buffer
       in.read(contentBytes, 0, this.downloadFileSize);
       out.write(contentBytes, 0, this.downloadFileSize);

code that puts file together:

        int partSize=0;
        //Create output stream
    OutputStream saveAs = new FileOutputStream(fileName);

        for(int i=0; i<filePieces;i++)
        {
           File file=new File("part_"+(i+1));
           partSize=(int)file.length();
            byte fileBuffer[]=new byte [partSize];
           //Create input stream
           InputStream is = new FileInputStream(file);
           is.read(fileBuffer);
           saveAs.write(fileBuffer);
           is.close();           
        }
user1205853
  • 651
  • 2
  • 8
  • 14

1 Answers1

2

Without further details and sample code you're forcing any answers to be guesses. Here are mine:

  • You're using Readers and Writers when you should use Input- / OutputStreams.
  • You've messed up the synchronization somehow. Favour classes from the java.util.concurrent package over home grown synchronized solutions.
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • here is a link to my project folder with the code. I am not very experienced with file writing so I am sure that is the issue. http://www.tempfiles.net/download/201202/230696/HttpDownloader.html – user1205853 Feb 19 '12 at 22:25
  • Updated your question with the relevant parts of the code and I'll have a look at it. – aioobe Feb 19 '12 at 22:26
  • OK. The first code is from Mythread.java and the lower is from the 'main' method in the httpdownloader,java – user1205853 Feb 19 '12 at 23:06