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?
 
     
     
     
    