Q:

incomplete download of file from url in java - using java NIO

I used the code provided in accepted solution of below thread to download a 500kb zip file
How to download and save a file from Internet using Java?
public static File  downloadFile(String fileURL, String saveDir)
        throws IOException {

    File downloadFolder = null;
    String saveFilePath = null;

    URL url = new URL(fileURL);
   ReadableByteChannel rbc = Channels.newChannel(url.openStream());
   String    fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length()); 
   FileOutputStream fos = new FileOutputStream(fileName);

   saveFilePath = saveDir + File.separator + fileName;
   downloadFolder = new File(saveDir);

   downloadFolder.deleteOnExit();
   downloadFolder.mkdirs();

   FileOutputStream outputStream = new FileOutputStream(saveFilePath);
   outputStream.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

   outputStream.close();
  rbc.close();
    return new File(saveFilePath);
}

The code is able to identify the file but the the problem is , the download is incomplete. It always downloads 5KB and stops there after. 
I dont want to use apache commons file untils.

A:

The problem was that I didn't set authentication. I set the authentication and it worked. code below
public static File  downloadFile(String fileURL, String saveDir)
    throws IOException {

File downloadFolder = null;
String saveFilePath = null;

String username = "guest";
String password = "guest";

String usepass = username + ":" + password;
String basicAuth = "Basic "+  javax.xml.bind.DatatypeConverter.printBase64Binary(usepass.getBytes());

URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) website.openConnection();
httpConn.setRequestProperty("Authorization", basicAuth);

ReadableByteChannel rbc = Channels.newChannel(httpConn.getInputStream());
String    fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,fileURL.length()); 
FileOutputStream fos = new FileOutputStream(fileName);

saveFilePath = saveDir + File.separator + fileName;
downloadFolder = new File(saveDir);

downloadFolder.deleteOnExit();
downloadFolder.mkdirs();

FileOutputStream outputStream = new FileOutputStream(saveFilePath);
outputStream.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

outputStream.close();
rbc.close();
return new File(saveFilePath);

}

