7

我使用此测试将 txt 转换为 pdf :

package convert.pdf;

//getResourceAsStream(String name) : Returns an input stream for reading the specified resource.
//toByteArray : Get the contents of an InputStream as a byte[].

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;

import convert.pdf.txt.TextConversion;

public class TestConversion {

  private static byte[] readFilesInBytes(String file) throws IOException {
      return IOUtils.toByteArray(TestConversion.class.getResourceAsStream(file));
  }

  private static void writeFilesInBytes(byte[] file, String name) throws IOException {
      IOUtils.write(file, new FileOutputStream(name));
  }

  //just change the extensions and test conversions
  public static void main(String args[]) throws IOException {
      ConversionToPDF algorithm = new TextConversion();
      byte[] file = readFilesInBytes("/convert/pdf/text.txt");
      byte[] pdf = algorithm.convertDocument(file);
      writeFilesInBytes(pdf, "text.pdf");
  }

}

问题:

线程“主”java.lang.NullPointerException 中的异常
    在 org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1025)
    在 org.apache.commons.io.IOUtils.copy(IOUtils.java:999)
    在 org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:218)
    在 convert.pdf.TestConversion.readFilesInBytes(TestConversion.java:17)
    在 convert.pdf.TestConversion.main(TestConversion.java:28)

我使用调试器,问题似乎出在此处:

  private static byte[] readFilesInBytes(String file) throws IOException {
      return IOUtils.toByteArray(TestConversion.class.getResourceAsStream(file));
  }

我的问题是什么?

4

4 回答 4

20

听起来该资源可能不存在该名称。

您是否知道Class.getResourceAsStream()找到与该类的包相关的资源,而ClassLoader.getResourceAsStream()没有?您可以使用前导正斜杠Class.getResourceAsStream()来模仿这一点,所以

Foo.class.getResourceAsStream("/bar.png")

大致相当于

Foo.class.getClassLoader().getResourceAsStream("bar.png")

实际上是您要加载的文件(即普通文件系统上的特定文件)吗?如果是这样,使用FileInputStream将是一个更好的选择。Class.getResourceAsStream()如果它是以其他方式捆绑在 jar 文件或类路径中的资源,则使用它;FileInputStream如果它是可以在文件系统中的任何位置的任意文件,则使用它。

编辑:另一件要小心的事情,这在以前给我造成了问题 - 如果这在你的开发盒上工作,恰好是 Windows,现在在恰好是 Unix 的生产服务器上失败,检查的情况文件名。不同的文件系统以不同的方式处理区分大小写的事实可能会很痛苦......

于 2009-03-31T21:58:22.747 回答
1

您是否在将文件传递给之前检查文件是否存在readFilesInBytes()?请注意,如果找不到文件,则Class.getResourceAsStream()返回。null你可能想做:

private static byte[] readFilesInBytes(String file) throws IOException {
  File testFile = new File(file);
  if (!testFile.exists()) {
      throw new FileNotFoundException("File " + file + " does not exist");
  }
  return IOUtils.toByteArray(TestConversion.class.getResourceAsStream(file));
}

或者更好:

private static byte[] readFilesInBytes(String file) throws IOException {
  InputStream stream = TestConversion.class.getResourceAsStream(file);
  if (stream == null) {
      throw new FileNotFoundException("readFilesInBytes: File " + file
                                      + " does not exist");
  }
  return IOUtils.toByteArray(stream);
}
于 2009-03-31T21:55:55.980 回答
0

该类读取类路径中的一个TXT文件,并使用TextConversion转换为PDF,然后将pdf保存在文件系统中。

这里的TextConversion代码:

package convert.pdf.txt;
//Conversion to PDF from text using iText.
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import convert.pdf.ConversionToPDF;
import convert.pdf.ConvertDocumentException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

public class TextConversion implements ConversionToPDF {

    public byte[] convertDocument(byte[] documents) throws ConvertDocumentException {
        try {
            return this.convertInternal(documents);
        } catch (DocumentException e) {
            throw new ConvertDocumentException(e);
        } catch (IOException e) {
            throw new ConvertDocumentException(e);
        }
    }

    private byte[] convertInternal(byte[] documents) throws DocumentException, IOException {
        Document document = new Document();

        ByteArrayOutputStream pdfResultBytes = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, pdfResultBytes);

        document.open();

        BufferedReader reader = new BufferedReader( new InputStreamReader( new ByteArrayInputStream(documents) ) );

        String line = "";
        while ((line = reader.readLine()) != null) {
            if ("".equals(line.trim())) {
                line = "\n"; //white line
            }
            Font fonteDefault = new Font(Font.COURIER, 10);
            Paragraph paragraph = new Paragraph(line, fonteDefault);
            document.add(paragraph);
        }

        reader.close();

        document.close();

        return pdfResultBytes.toByteArray();
    }
}

这里是 ConversionToPDF 的代码:

package convert.pdf;
// Interface implemented by the conversion algorithms.
public interface ConversionToPDF {

    public byte[] convertDocument(byte[] documentToConvert) throws ConvertDocumentException;
    }

我认为问题来自我的文件系统(Windows 和服务器上的 devbox 是 Unix)。我将尝试修改我的类路径。

于 2009-03-31T22:20:49.710 回答
0

这个问题可能是调用方法 on 引起的test.txt,可以是文件夹快捷方式。换句话说,您正在对不存在的文件调用方法,从而导致NullPointerException.

于 2012-08-02T01:20:50.517 回答