【发布时间】:2011-01-27 12:57:00
【问题描述】:
我想从 url 读取 pdf 文件并将其转换为缩略图。我正在使用以下代码。我没有在这里包含转换部分。问题出在“pdffile = new PDFFile(buf);”这一行上我收到一个异常“java.io.IOException:这可能不是 PDF 文件”。但是我可以在浏览器上看到pdf。我是怎么了?请帮我。
byte[] byteArray = null;
InputStream is = null;
String streamTo = null;
BufferedImage bmg = null;
PDFFile pdffile;
ByteBuffer buf;
int pageNumber = 1;
try {
is = fetchImageFromServer(url); //Pdf Url path
if (!pageNumber.isEmpty()) {
streamTo = is.toString();
byteArray = streamTo.getBytes();
buf = ByteBuffer.wrap(byteArray);
pdffile = new PDFFile(buf);
}
} catch (IOExceptio e) {
}
【问题讨论】:
-
您应该提及您正在使用哪个库来执行此操作 -
PDFFile不是标准 Java 类。 -
试试这篇文章 - How To Read A PDF File From A URL In Java。我是为我公司的 Java PDF 组件套件编写的,但它可以满足您的要求。
-
我在堆栈溢出方面比较新鲜。我不知道更多。如果我有任何错误或对我的任何建议,请提供帮助。
-
+1,但代码中存在轻微错误:带有空块的额外 while:while ((baLength = is1.read(ba1)) != -1) { fos1.write( ba1, 0, baLength); } while (baLength != -1);
-
谢谢你,莫里斯。我很惊讶它在没有警告的情况下编译。我已经修复了错误。