#android
#Android
Вопрос:
У меня есть ссылка на сервер, откуда мне нужно прочитать содержимое 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):
Ответ №1:
Посмотрите на следующий код для сохранения файла 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();
Спасибо
Сунил