Skip to content

Commit

Permalink
Download files fully before write to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
eirslett committed Oct 25, 2023
1 parent 49e11d1 commit 5e712eb
Showing 1 changed file with 4 additions and 4 deletions.
Expand Up @@ -9,6 +9,7 @@
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.http.HttpHost;
Expand Down Expand Up @@ -71,11 +72,10 @@ public void download(String downloadUrl, String destination, String userName, St
if(statusCode != 200){
throw new DownloadException("Got error code "+ statusCode +" from the server.");
}

byte[] data = IOUtils.toByteArray(response.getEntity().getContent());
new File(FilenameUtils.getFullPathNoEndSeparator(destination)).mkdirs();
ReadableByteChannel rbc = Channels.newChannel(response.getEntity().getContent());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
FileUtils.writeByteArrayToFile(new File(destination), data);
}
} catch (IOException | URISyntaxException e) {
throw new DownloadException("Could not download " + fixedDownloadUrl, e);
Expand Down

0 comments on commit 5e712eb

Please sign in to comment.