2016-02-28 34 views
0

使用下面的代碼實現Tika(文章對象是我自己的),我遇到了URL重定向到最後一頁,我相信通過jQuery.extend命令。Apache Tika - 如何訪問重定向URL

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獲得實際的最終網址?

具有重定向在它的一個例子網址是:

http://sbs.feedsportal.com/c/34692/f/637529/s/4d7e2cd0/sc/14/l/0L0Ssbs0N0Bau0Cnews0Carticle0C20A160C0A20C110Cscientists0Emaking0Ezika0Edetection0Ekits/story01.htm--2016-02-27

回答

1

基於這樣的回答:https://stackoverflow.com/a/5270162/4471711

我用下面的代碼:

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()返回新的(重定向)網址。