Answer the question
In order to leave comments, you need to log in
Why is it giving a 302 error?
Good to everyone!
I'm trying to download a file, but I'm getting a 302 Found error.
package download;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
public class JavaDownloadFileFromURL {
public static void main(String[] args) {
String url = "http://opendata.fssprus.ru/7709576929-iplegallist/data-20210327-structure-20200401.csv";
try {
downloadUsingNIO(url, "doc.сsv");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void downloadUsingNIO(String urlStr, String file) throws IOException {
System.setProperty("http.agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36");
URL url = new URL(urlStr);
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(file);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question