O
O
OctorberMyMonth2021-03-27 16:39:10
Java
OctorberMyMonth, 2021-03-27 16:39:10

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

}

How can I fix this code to download the file.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-03-27
@OctorberMyMonth

Replace the link with this one:

https://opendata.fssp.gov.ru/opendata.php?path=/opendata//7709576929-iplegallist/data-20210327-structure-20200401.csv

302 is a temporary redirect to another URL.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question