2011-05-31 77 views
2

我有一個服務器鏈接,我需要閱讀pdf內容。我使用了下面的代碼,但它給出了其他格式的結果。如何從服務器讀取pdf內容並保存在文件中?

public String readPDF() throws Exception 
{ 
    BufferedReader in = null; 
    String page = ""; 
    try { 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet request = new HttpGet(); 
     request.setURI(new URI("http://14.140.41.194/monali/i.pdf")); 
     HttpResponse response = client.execute(request); 
     in = new BufferedReader 
     (new InputStreamReader(response.getEntity().getContent())); 
     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 
     String NL = System.getProperty("line.separator"); 
     while ((line = in.readLine()) != null) { 
      sb.append(line + NL); 
     } 
     in.close(); 
     page = sb.toString(); 
     } finally { 
     if (in != null) { 
      try { 
       in.close(); 
      } catch (IOException e) {e.printStackTrace();} 
     } 
    }   
    return page;  
} 

05-31 19:00:49.894: INFO/System.out(624): page .. %PDF-1.4 
05-31 19:00:49.894: INFO/System.out(624): %���� 
05-31 19:00:49.903: INFO/System.out(624): 15 0 obj 
05-31 19:00:49.903: INFO/System.out(624): <</Length 2496 
05-31 19:00:49.916: INFO/System.out(624): /Subtype /XML 
05-31 19:00:49.916: INFO/System.out(624): /Type /Metadata 
05-31 19:00:49.916: INFO/System.out(624): >> 
05-31 19:00:49.916: INFO/System.out(624): stream 
05-31 19:00:49.916: INFO/System.out(624): <?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?> 
05-31 19:00:49.916: INFO/System.out(624): <x:xmpmeta x:xmptk="3.1-701" xmlns:x="adobe:ns:meta/"> 
05-31 19:00:49.916: INFO/System.out(624): <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> 
05-31 19:00:49.916: INFO/System.out(624):  <rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/"> 
05-31 19:00:49.916: INFO/System.out(624):  <xmp:CreateDate>2011-05-20T05:46:02Z</xmp:CreateDate> 
05-31 19:00:49.916: INFO/System.out(624):  <xmp:CreatorTool>Nitro PDF Professional (6, 0, 1, 8)</xmp:CreatorTool> 
05-31 19:00:49.916: INFO/System.out(624):  <xmp:ModifyDate>2011-05-20T05:46:04Z</xmp:ModifyDate> 
05-31 19:00:49.916: INFO/System.out(624):  <xmp:MetadataDate>2011-05-20T05:46:04Z</xmp:MetadataDate> 
05-31 19:00:49.916: INFO/System.out(624):  </rdf:Description> 
05-31 19:00:49.916: INFO/System.out(624):  <rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/"> 
05-31 19:00:49.916: INFO/System.out(624):  <dc:format>application/pdf</dc:format> 
05-31 19:00:49.916: INFO/System.out(624):  <dc:creator> 
05-31 19:00:49.916: INFO/System.out(624):   <rdf:Seq> 
05-31 19:00:49.916: INFO/System.out(624):   <rdf:li></rdf:li> 
05-31 19:00:49.916: INFO/System.out(624):   </rdf:Seq> 
05-31 19:00:49.916: INFO/System.out(624):  </dc:creator> 
05-31 19:00:49.916: INFO/System.out(624):  <dc:title> 
05-31 19:00:49.916: INFO/System.out(624):   <rdf:Alt> 
05-31 19:00:49.916: INFO/System.out(624):   <rdf:li xml:lang="x-default"></rdf:li> 
05-31 19:00:49.916: INFO/System.out(624):   </rdf:Alt> 
05-31 19:00:49.916: INFO/System.out(624):  </dc:title> 
05-31 19:00:49.916: INFO/System.out(624):  <dc:description> 
05-31 19:00:49.916: INFO/System.out(624):   <rdf:Alt> 
05-31 19:00:49.916: INFO/System.out(624):   <rdf:li xml:lang="x-default"/> 
05-31 19:00:49.916: INFO/System.out(624):   </rdf:Alt> 
05-31 19:00:49.916: INFO/System.out(624):  </dc:description> 
05-31 19:00:49.916: INFO/System.out(624):  </rdf:Description> 
05-31 19:00:49.916: INFO/System.out(624):  <rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/"> 
05-31 19:00:49.916: INFO/System.out(624):  <pdf:Keywords></pdf:Keywords> 
05-31 19:00:49.916: INFO/System.out(624):  <pdf:Producer>Nitro PDF Professional (6, 0, 1, 8)</pdf:Producer> 
05-31 19:00:49.916: INFO/System.out(624):  </rdf:Description> 
05-31 19:00:49.916: INFO/System.out(624):  <rdf:Description rdf:about="" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/"> 
05-31 19:00:49.916: INFO/System.out(624):  <xmpMM:DocumentID>uuid:3e7ef8a9-a526-45bf-9db1-a31533c20f86</xmpMM:DocumentID> 
05-31 19:00:49.916: INFO/System.out(624):  </rdf:Description> 
05-31 19:00:49.916: INFO/System.out(624): </rdf:RDF> 
05-31 19:00:49.916: INFO/System.out(624): </x:xmpmeta> 
05-31 19:00:49.916: INFO/System.out(624):                          
05-31 19:00:49.916: INFO/System.out(624):                          

回答

3

對下面的代碼來看看存儲PDF文件

try { 

         URL u = new URL("http://14.140.41.194/monali/i.pdf"); 
         HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
         c.setRequestMethod("GET"); 
         c.setDoOutput(true); 
         c.connect(); 
         String fileName = Environment.getExternalStorageDirectory().getAbsolutePath(); 
         FileOutputStream f = new FileOutputStream(new File(fileName,"my.pdf")); 


         InputStream in = c.getInputStream(); 

         byte[] buffer = new byte[1024]; 
         int len1 = 0; 
         while ((len1 = in.read(buffer)) > 0) { 
          f.write(buffer,0, len1); 
        } 

         f.close(); 

感謝 蘇尼爾

+1

感謝迪帕克..它爲我工作 – Monali 2011-05-31 14:08:58

+1

謝謝你的工作:) – 2015-01-13 12:56:50

相關問題