2 ssummeraw ssummeraw 于 2013.04.25 13:23 提问

如何从服务器中读取 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;       
}

输出:

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

1个回答

lyyybz
lyyybz   Rxr 2016.08.17 16:30

直接创建个文件,写到文件中不行么?

Csdn user default icon
上传中...
上传图片
插入图片