5

I'm making a program that will download files from URL. The downloading always starts, but it is not completed. For example, if file's size is 3 MB, program download only half of that so I cannot open the downloaded file. But program says that file is downloaded succesfully.

public class FileDownloader {

    public static void main (String [] args) throws IOException {

        InputStream fileIn;
        FileOutputStream fileOut;
        Scanner s = new Scanner(System.in);

        System.out.println("Enter URL: ");
        String urlStr = s.nextLine();

        URL url = new URL(urlStr);
        URLConnection urlConnect = url.openConnection();
        fileIn = urlConnect.getInputStream();

        System.out.println("Enter file name: ");
        String fileStr = s.nextLine();
        fileOut = new FileOutputStream(fileStr);

        while (fileIn.read() != -1) {   
            fileOut.write(fileIn.read());
        }
        System.out.println("File is downloaded");
    }
}

So how can I solve it? Should use another way to download?

moffeltje
  • 4,521
  • 4
  • 33
  • 57
Azamat Salamat
  • 57
  • 2
  • 10
  • Possible duplicate of [How to download and save a file from Internet using Java?](https://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java) – Robin Green Nov 17 '18 at 06:31

3 Answers3

5

You are losing every alternate bytedue to

    while (fileIn.read() != -1) {     //1st read
        fileOut.write(fileIn.read());     //2nd read - 1st write
    }

You are reading twice and writing only once.

What you need to do is

    int x;
    while ((x = fileIn.read()) != -1) {   //1st read
        fileOut.write(x);     //1st write
    }

Here is your complete code

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

public class FileDownloader {

    public static void main(String[] args) throws IOException {

        InputStream fileIn;
        FileOutputStream fileOut;
        Scanner s = new Scanner(System.in);

        System.out.println("Enter URL: ");
        String urlStr = s.nextLine();

        URL url = new URL(urlStr);
        URLConnection urlConnect = url.openConnection();
        fileIn = urlConnect.getInputStream();

        System.out.println("Enter file name: ");
        String fileStr = s.nextLine();
        fileOut = new FileOutputStream(fileStr);

        int x;
        while ((x = fileIn.read()) != -1) {
            fileOut.write(x);
        }
        System.out.println("File is downloaded");

}
Shrinivas Shukla
  • 4,325
  • 2
  • 21
  • 33
1

You can download a large file with below code efficiently.

 public static void main(String[] args) throws IOException {
    InputStream in = null;
    FileOutputStream out = null;
    try {
        System.out.println("Starting download");
        long t1 = System.currentTimeMillis();
        URL url = new URL(args[0]);// or you can hard code the URL
        // Open the input and out files for the streams
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        in = conn.getInputStream();
        out = new FileOutputStream(args[1]);//// or you can hard code the filename
        // Read data into buffer and then write to the output file
        byte[] buffer = new byte[8192];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        long t2 = System.currentTimeMillis();
        System.out.println("Time for download & save file in millis:"+(t2-t1));
    } catch (Exception e) {
        // Display or throw the error
        System.out.println("Erorr while execting the program: "
                + e.getMessage());
    } finally {
        // Close the resources 
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }

}
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
0

just simply use this:

import org.apache.commons.io.FileUtils;
import java.net.URL;
String path =  "F:/"
String fileName =  "song"
FileUtils.copyURLToFile(myUrl, new File(path + fileName + ".mp3"));
Jayhello
  • 5,931
  • 3
  • 49
  • 56