Apache Tika - 如何访问重定向 URL
Apache Tika - How to access a redirect URL
使用下面的代码片段实现 Tika(文章对象是我自己的),我遇到了 URL 的重定向到最终页面,我相信通过 jQuery.extend命令。
URL articleURL = new URL(article.getLink());
stream = TikaInputStream.get(articleURL);
articleBytes = IOUtils.toByteArray(stream);
if (articleBytes.length == 0) {
return null;
} else {
article.setContentLength((long) articleBytes.length);
}
ContentHandler textHandler = new BodyContentHandler();
Metadata metadata = new Metadata();
AutoDetectParser parser = new AutoDetectParser();
ParseContext context = new ParseContext();
parser.parse(new ByteArrayInputStream(articleBytes), new BoilerpipeContentHandler(textHandler), metadata, context);
Tika 遵循重定向 URL 就好了,但是我想知道最终的 URL 是什么。有什么方法可以从 Tika 获得实际的、最终的 URL?
其中包含重定向的示例 URL 是:
基于这个答案:
我使用了以下代码:
URLConnection con = new URL(article.getLink()).openConnection();
con.connect();
stream = TikaInputStream.get(con.getInputStream());
articleBytes = IOUtils.toByteArray(stream);
article.setLink(con.getURL().toExternalForm());
con.getURL().toExternalForm() 返回了新的(重定向的)url.
使用下面的代码片段实现 Tika(文章对象是我自己的),我遇到了 URL 的重定向到最终页面,我相信通过 jQuery.extend命令。
URL articleURL = new URL(article.getLink());
stream = TikaInputStream.get(articleURL);
articleBytes = IOUtils.toByteArray(stream);
if (articleBytes.length == 0) {
return null;
} else {
article.setContentLength((long) articleBytes.length);
}
ContentHandler textHandler = new BodyContentHandler();
Metadata metadata = new Metadata();
AutoDetectParser parser = new AutoDetectParser();
ParseContext context = new ParseContext();
parser.parse(new ByteArrayInputStream(articleBytes), new BoilerpipeContentHandler(textHandler), metadata, context);
Tika 遵循重定向 URL 就好了,但是我想知道最终的 URL 是什么。有什么方法可以从 Tika 获得实际的、最终的 URL?
其中包含重定向的示例 URL 是:
基于这个答案:
我使用了以下代码:
URLConnection con = new URL(article.getLink()).openConnection();
con.connect();
stream = TikaInputStream.get(con.getInputStream());
articleBytes = IOUtils.toByteArray(stream);
article.setLink(con.getURL().toExternalForm());
con.getURL().toExternalForm() 返回了新的(重定向的)url.