简体   繁体   English

“ java.io.IOException:这可能不是PDF文件”

[英]“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.

相关问题 Avro - java.io.IOException:不是数据文件 - Avro - java.io.IOException: Not a data file java.io.IOException: error=2, 没有那个文件或目录 - java.io.IOException: error=2, No such file or directory java.io.IOException:找不到文件 - java.io.IOException: File Not Found java.io.IOException: 无法运行程序“...”: java.io.IOException: error=2, No such file or directory - java.io.IOException: Cannot run program “…”: java.io.IOException: error=2, No such file or directory java.io.IOException:java.io.FileNotFoundException :(无此类文件或目录) - java.io.IOException: java.io.FileNotFoundException:(No such file or directory) java.io.IOException:使用 .cer 文件对 pdf 进行数字签名时获取的密钥库格式无效 - java.io.IOException: Invalid keystore format getting while signing pdf digitally using .cer file java.io.IOException java - java.io.IOException java MapReduce 中的 java.io.IOException - java.io.IOException in MapReduce File.createTempFile(错误:java.io.IOException:没有这样的文件或目录) - File.createTempFile (Error: java.io.IOException: No such file or directory) Java.io.IOException:错误= 2,在Java中执行curl时没有此类文件或目录 - Java.io.IOException: error=2, No such file or directory on executing curl in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM