I need to download a file .zip
that can be found at the following address: link
For this I created the following method:
public static void downloadFile() throws IOException{
final File file = new File("download/");
if(!file.exists()){
file.mkdirs();
}
final URL link = new URL(URL);
final InputStream in = new BufferedInputStream(link.openStream());
final ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream(PATH);
fos.write(response);
fos.close();
}
But when it runs link.openStream()
, the following error occurs:
java.net.ProtocolException: Server redirected too many times (20)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at good.dowload.DownloadManager.downloadFile(DownloadManager.java:31)
at good.dowload.DownloadManager.main(DownloadManager.java:19)
How can I circumvent these redirects?