1

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.

Community
  • 1
  • 1
Red Ant
  • 315
  • 4
  • 17
  • Are you able to get the size of the file you download? – dARKpRINCE Dec 12 '13 at 11:08
  • do you mean reading the file size before downloading? – Red Ant Dec 12 '13 at 11:24
  • Sorry you can't read the size of a ReadableByteChannel. Use read() method until the return value is -1 and write the buffer into a file. – dARKpRINCE Dec 12 '13 at 11:34
  • actually , i used traditional way of reading to buffer until -1 , it also failed for the same reason. only 5kb download . i gave a try with apache commons io (just to experiment) , even that stops at 5k download.I think something else is causing the issue. – Red Ant Dec 12 '13 at 11:49
  • When downloading with browser, its more than 5KB? – dARKpRINCE Dec 12 '13 at 12:03
  • yes, browser does complete file download. – Red Ant Dec 12 '13 at 12:15
  • Sorry bro can't help you further. Seems to be something non-code related. – dARKpRINCE Dec 12 '13 at 13:00
  • its my fault. my browser was auto-logged in while i was downloading the file. when java accessed it it was downloading the login page and hence always 5 KB. I have to figure out how to set authentication NIO approach. For now i have solved the problem by traditional way - read from stream after setting the authentication. Do you know how do i set basic auth for the code i pasted. thanks – Red Ant Dec 12 '13 at 17:01
  • Please post a separate question for that. But before ensure you have searched StackOverflow for similar questions. – dARKpRINCE Dec 13 '13 at 09:17
  • I solved it , I passed the Inputstream from httpurl connection to channel after setting basic auth. it worked. thanks. – Red Ant Dec 16 '13 at 07:19

1 Answers1

1

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);

}

Red Ant
  • 315
  • 4
  • 17