如何在java中下载非直接链接

时间:2013-11-23 22:02:10

标签: java

我正在尝试下载非直接链接(我不知道它是如何被称为)。

这就是:

从资源管理器中我只需点击下载链接即可。但是,如果我将链接复制到地址栏,它会重定向到其他页面。

我有这个问题的地方就是这个页面: http://www.subtitulos.es/show/408

我需要的所有链接都是“descargar”

如果我尝试下载该链接,则会下载该页面的代码。 我该如何下载文件?这是副标题.srt

我的代码:

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

谢谢!

2 个答案:

答案 0 :(得分:0)

您需要将响应从http://www.subtitulos.es/updated/1/33565/0转换为String。 然后,您可以使用正则表达式在srt文件上查找直接链接,如:

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 
}

我建议您使用http://commons.apache.org/proper/commons-io/http://hc.apache.org/httpclient-3.x/个库。有很多样本。

答案 1 :(得分:0)

这是我为你的案例修改过的mkyong https://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/的另一种方法:

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

...