Приведенный ниже код работал для меня, есть 2 способа:
1-й метод: (Вам необходимо скачать JAR AsyncHttpClient)
try {
AsyncHttpClient client = Dsl.asyncHttpClient();
final FileOutputStream stream = new FileOutputStream(FILE_NAME);
client.prepareGet(FILE_URL).execute(new AsyncCompletionHandler<FileOutputStream>() {
@Override
public State onBodyPartReceived(HttpResponseBodyPart bodyPart)
throws Exception {
stream.getChannel().write(bodyPart.getBodyByteBuffer());
return State.CONTINUE;
}
@Override
public FileOutputStream onCompleted(Response response)
throws Exception {
return stream;
}
});
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2-й метод:
private static void startDownload(String FILE_URL, String FILE_NAME)
{
try (BufferedInputStream in = new BufferedInputStream(new URL(FILE_URL).openStream());
FileOutputStream fileOutputStream = new FileOutputStream(FILE_NAME)) {
byte dataBuffer[] = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
} catch (IOException e) {
System.out.println(e);
}
}