2010-10-25 57 views
0

我已經使用iText解析pdf文件。它的工作原理以及對本地文件,但我想分析其在Web服務器託管的這樣一個PDF文件:解析網絡服務器託管的PDF文件

「http://protege.stanford.edu/publications/ontology_development/ontology101.pdf」

但我不知道如何?您能否請我回答如何使用iText或其他庫來執行此任務... thx

+0

您可以從URL /中讀取文件。 – 2010-10-25 08:49:16

回答

0

您需要下載PDF文件的字節。你可以這樣做:

URL url = new URL("http://....."); 
URLConnection conn = url.getConnection(); 

if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { ..error.. } 
if (! conn.getContentType().equals("application/pdf")) { ..error.. } 

InputStream byteStream = conn.getInputStream(); 
try { 
    ... // give bytes from byteStream to iText 
} finally { byteStream.close(); } 
0

使用URLConnection類:

URL reqURL = new URL("http://www.mysite.edu/mydoc.pdf"); 
URLConnection urlCon = reqURL.openConnection(); 

然後你可以使用URLConnection方法來檢索內容。最簡單的方法:

InputStream is = urlCon.getInputStream(); 
byte[] b = new byte[1024]; //size of a buffer, can be any 
int len; 
while((len = is.read(b)) != -1){ 
    //Store the content in preferred way 
} 
is.close(); 
0

沒什麼。您可以直接通過一個URL進入PdfReader,並讓它處理的數據流爲您提供:

URL url = new URL("http://protege.stanford.edu/publications/ontology_development/ontology101.pdf"); 
PdfReader reader = new PDFReader(url); 

The JavaDoc is your friend

+0

認爲 的問題是:當我在一個HTML的網站執行的代碼,它工作得很好,但是當我執行它在PDF文檔網站,獲得奇怪的文字是這樣的: £$'EA」 ?-ÕUø4¸s·UTD×SEI• Rim 2010-10-25 10:56:36

+0

PDF通常包含二進制數據,這是正常的。圖像,字體,壓縮的內容流,你的名字。 – 2010-10-25 18:19:47

相關問題