[英]“java.io.IOException: This may not be a PDF File”
I want to read a pdf file from a url and convert it into a thumbnail image. 我想从网址中读取pdf文件,然后将其转换为缩略图。 I am using the following code.
我正在使用以下代码。 I didnt included the converting portion here.The problem is on the line "pdffile = new PDFFile(buf);"
我没有在此处包括转换部分。问题出在“ pdffile = new PDFFile(buf);”这一行上。 I get an exception "java.io.IOException: This may not be a PDF File".
我收到异常“ java.io.IOException:这可能不是PDF文件”。 But I can see the pdf on the browser.
但是我可以在浏览器上看到pdf。 What is wrong with me?
我是怎么了? Please help me.
请帮我。
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) {
}
You must read the content of the stream. 您必须阅读流的内容。 toString will not do that.
toString不会那样做。
The is.toString()
call won't read all the bytes correctly. is.toString()
调用不会正确读取所有字节。 There is an utility function at Apache Commons IO that will help you, IOUtils.toByteArray() . Apache Commons IO上有一个实用程序函数IOUtils.toByteArray() 。 Try this:
尝试这个:
is = fetchImageFromServer(url); //Pdf Url path
if (!pageNumber.isEmpty()) {
byteArray = IOUtils.toByteArray(is);
buf = ByteBuffer.wrap(byteArray);
pdffile = new PDFFile(buf);
}
PDf is a binary object. PDf是一个二进制对象。 If you convert it to a string, it will change byte values and break the file.
如果将其转换为字符串,它将更改字节值并破坏文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.