java - 如何在初始化时在类的构造函数调用中容纳一个 FileInputStream 对象为 InputStream 对象?

标签 java file-io coldfusion cfml default-constructor

1 创建并初始化 myDocDesigner 对象:

这里是 myDocDesignerclass 的定义,其构造函数为:

public myDocDesigner(customDocFormat doc, InputStream imageStream, int page) throws IOException 
{
    docImageStream(imageStream);
    calculatePageSize(doc, page);
}

2 来自 myFileManager 类中的 generateOutputDoc() 如下:

jMyDocDesigner = createObject("java"
                            ,"myPackage.visible.myDocDesigner")
                            .init(jMyLocalDoc ,myImageFileStream,1);

3 属性定义为:

/* load document : `customDocFormat` isn't only a format it also returns the document object of the type `customDocFormat` to be used in #1 & #2 above */

    //document & image file paths
    lStrInputFilePath = "#expandPath('sampledocs')#\mySampleDoc.pdf";
    lStrImageFilePath = "#expandPath('sampledocs')#\myPlacementLogo.png";
    //java FileInputStream object for the PDF file
    lOsPDFInput = CreateObject("java","java.io.FileInputStream")
                     .init(CreateObject("java","java.io.File").init(lStrInputPDFfile));
    jMyLocalDoc = createObject("java","myPackage.docModel.customDocFormat");
    jMyLocalDoc.load(lOsPDFInput);
    //FileInputStream Object for image
    lSignImage  = CreateObject("java","java.io.FileOutputStream")
                      .init(CreateObject("java","java.io.File").init(lStrImageFilePath));

4 问题:

为了清楚起见,让事情变得简单:我已经尽我对 Java 的了解尝试了所有可能的选择。但我无法让这个工作。这部分代码是项目的一部分,用于根据一些内部要求和约定来操作文档。每件事都工作完美...我对整个代码进行了逐步调试,但这部分阻碍了一切...我尝试了 4-5 个变体来转换 FileInputStream 对象并将其传递给构造函数 InputStream 但每次我都会收到此错误,这是可以理解的,因为它不接受我向它抛出的参数:

enter image description here

我更新了我的问题,因为我发现问题的原因在于我按照 Leigh 所指出的初始化的 FileInputStream 对象本身。在他的回答中,我发现我没有走正确的路,多亏了他......我能够继续我的代码,这是我的代码中唯一阻止项目最终确定的错误:(:

The <code>PDDocument</code> after loading the original Input document via <code>FileInputStream</code> object has this value of <code>int</code> field <code>UNKNOWN_NUMBER_OF_PAGES</code> as <code>-1</code> every time and to cross-verify that I also called <code>getPageCount()</code> function which returns a value of <code>0</code> irrespective of the no. of pages in my PDF document and no matter how many types of document I use....the surprising thing is that the stream is correctly fetched which might look like the problem in this case...because I was able to construct the duplicate copy using the stream of bytes I read from the stream object. Please help me on this! SOS

以下是我处理的 PDDocument 和 PDDocumentInformation 类的 Java 文件,我得到了错误的编号。通过它加载文档后的页面数:go here

最佳答案

你可以试试这个:

lOsPDFInputStream = CreateObject("java","java.io.InputStream");
lOsPDFInputStream = lOsPDFInput;

现在尝试再次传递 lOsPDFInput,因为现在它是 InputStream 的对象。

您还可以尝试将FileInputStream转换为InputStream:

  public void givenUsingPlainJava_whenConvertingFileToInputStream_thenCorrect() 
  throws IOException {
    File initialFile = new File("src/main/resources/sample.txt");
    InputStream targetStream = new FileInputStream(initialFile);
}

引用号:http://www.baeldung.com/convert-file-to-input-stream

请告诉我它是否适合您。

关于java - 如何在初始化时在类的构造函数调用中容纳一个 FileInputStream 对象为 InputStream 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29115943/

相关文章:

java - LocalDateTime 时区有效,而 LocalTime 无效

java - Android - 1 除以 2 = 0

c - 如何在 C 中打开或创建具有权限的 .out 文件

coldfusion - 我们可以通过 URL 传递整个结构吗?

mysql - 无法创建与数据库的连接 (ColdFusion)

java - Kotlin:将日期字符串转换为 ISO 字符串

java - JVM 启动器找不到主类

javascript - 遇到EOF时如何不停止读取文件?

c# - 返回 FileStream 后无法删除临时文件

javascript - 在这么多行之后结束表并开始新表