0

I'm trying to download a non-direct link (I don't know how it's really called).

This is what happens:

From the explorer I just need to click the link to download. But if I copy the link to the address bar, it redirect to other page.

Where I have this problem is for example this page: http://www.subtitulos.es/show/408

All the links I need are on "descargar"

If I try to download the link, it downloads the code of the page. How can I download the file?? It's a subtitle .srt

The code I have:

final URL poster = new URL("http://www.subtitulos.es/updated/1/33565/0");

final ReadableByteChannel rbc = Channels.newChannel(poster.openStream());

final FileOutputStream fos = new FileOutputStream("C:/Users/Ricardo/Downloads/a.srt");

fos.getChannel().transferFrom(rbc, 0, 1 << 24);

fos.close();

rbc.close();

Thanks!!

2 Answers2

0

You need to convert response from http://www.subtitulos.es/updated/1/33565/0 to String. Then You can use regular expression to find direct links on srt files like:

Matcher m = Pattern.compile("(?<=<a href=\")http://.*(?=\">descargar</a>)").matcher(responseString);
while (m.find()) {
    String srtUrl = m.group();
    // here insert your code to download srt file 
}

I would reccomend you to use http://commons.apache.org/proper/commons-io/ and http://hc.apache.org/httpclient-3.x/ libraries. There are a lot of samples.

StrekoZ
  • 618
  • 6
  • 12
0

This is another method from mkyong https://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/ that I have modified for your case:

final URL poster = new URL("http://www.subtitulos.es/updated/1/33565/0");

HttpURLConnection conn = (HttpURLConnection) poster.openConnection();
conn.setReadTimeout(5000);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");

System.out.println("Request URL ... " + url);

boolean redirect = false;

// normally, 3xx is redirect
int status = conn.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
    if (status == HttpURLConnection.HTTP_MOVED_TEMP
        || status == HttpURLConnection.HTTP_MOVED_PERM
            || status == HttpURLConnection.HTTP_SEE_OTHER)
    redirect = true;
}

System.out.println("Response Code ... " + status);

if (redirect) {

    // get redirect url from "location" header field
    String newUrl = conn.getHeaderField("Location");

    // get the cookie if need, for login
    String cookies = conn.getHeaderField("Set-Cookie");

    // open the new connnection again
    conn = (HttpURLConnection) new URL(newUrl).openConnection();
    conn.setRequestProperty("Cookie", cookies);
    conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
    conn.addRequestProperty("User-Agent", "Mozilla");
    conn.addRequestProperty("Referer", "google.com");

    System.out.println("Redirect to URL : " + newUrl);
}

final ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream());

...
Dylan Smith
  • 491
  • 5
  • 11