gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:55:16 24 4
gpt4 key购买 nike

1 为 myDocDesigner 创建和初始化对象:

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

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

2 来自 generateOutputDoc()myFileManager分类为:

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

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com