我使用此测试将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");
}
}
Run Code Online (Sandbox Code Playgroud)
问题:
Exception in thread "main" java.lang.NullPointerException
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1025)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:999)
at org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:218)
at convert.pdf.TestConversion.readFilesInBytes(TestConversion.java:17)
at convert.pdf.TestConversion.main(TestConversion.java:28)
我使用调试器,问题似乎位于这里:
private static byte[] readFilesInBytes(String file) throws IOException {
return IOUtils.toByteArray(TestConversion.class.getResourceAsStream(file));
}
Run Code Online (Sandbox Code Playgroud)
我的问题是什么?
Jon*_*eet 19
听起来这个名称可能不存在资源.
您是否知道Class.getResourceAsStream()找到相对于该类包的资源,而ClassLoader.getResourceAsStream()不是?您可以使用前导斜杠Class.getResourceAsStream()来模仿这个,所以
Foo.class.getResourceAsStream("/bar.png")
Run Code Online (Sandbox Code Playgroud)
大致相当于
Foo.class.getClassLoader().getResourceAsStream("bar.png")
Run Code Online (Sandbox Code Playgroud)
这实际上是您尝试加载的文件(即普通文件系统上的特定文件)吗?如果是这样,使用FileInputStream将是一个更好的选择.使用Class.getResourceAsStream(),如果它在一个jar文件中或以其他方式classpath中捆绑的资源; 使用FileInputStream如果它可能是在文件系统中的任何地方任意文件.
编辑:另外要注意的事情,这在我之前引起了我的问题 - 如果这对你的开发盒有效,恰好是Windows,并且现在在生产服务器上失败了,恰好是Unix,检查一下文件名.不同的文件系统以不同的方式处理区分大小写的事实可能是一种痛苦......
您是否在将文件传递给之前检查该文件是否存在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));
}
Run Code Online (Sandbox Code Playgroud)
或者更好:
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28767 次 |
| 最近记录: |