使用java方法合并pdf并获取错误

时间:2016-07-20 20:02:48

标签: java coldfusion coldfusion-10

我有一段创建问题的代码。我用于合并的以下代码如下:

<cfset inFiles = [#convertArray#]>
<cfset outFile = "c:\merged_1.pdf">
<cfscript>
try {
    outStream = createObject("java", "java.io.FileOutputStream").init( outFile );
    copy = createObject("java", "com.lowagie.text.pdf.PdfCopyFields").init(outStream);
    PdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader");
    for (inFile in inFiles) {
        reader = PdfReader.init( inFile );
        copy.addDocument(reader);
        //writedump(inFile);
    }
    WriteOutput("Finished!");
}
catch (java.language.Exception de) {
    writedump(de);
}
if (IsDefined("copy")) {
    copy.close();
}
if (IsDefined("outputStream")) {
    outputStream.close();
}
</cfscript>

convertArray是像这样的文件列表

"C:\ColdFusion10\cfusion\wwwroot\_pdf\YEF5.pdf","C:\ColdFusion10\cfusion\wwwroot\_pdf\WEX9.pdf","C:\ColdFusion10\cfusion\wwwroot\_pdf\1WFX8.pdf","C:\ColdFusion10\cfusion\wwwroot\_pdf\WFY6.pdf","C:\ColdFusion10\cfusion\wwwroot\_pdf\1WFZ3.pdf"

如果我传递硬编码值,它会起作用,但如果我将其作为convertArray发送,则会抛出以下错误

The web site you are accessing has experienced an unexpected error.
Please contact the website administrator. 

The following information is meant for the website developer for debugging purposes.
Error Occurred While Processing Request
"C:/ColdFusion10/cfusion/wwwroot/_pdf/YEF5.pdf","C:/ColdFusion10/cfusion/wwwroot/_pdf/0WEX9.pdf","C:/ColdFusion10/cfusion/wwwroot/_pdf/1WFX8.pdf","C:/ColdFusion10/cfusion/wwwroot/_pdf/1WFY6.pdf","C:/ColdFusion10/cfusion/wwwroot/_pdf/1WFZ3.pdf" not found as file or resource.


The error occurred in C:/ColdFusion10/cfusion/wwwroot/index.cfm: line 30
28 :    PdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader");
29 :    for (inFile in inFiles) {
30 :        reader = PdfReader.init( inFile );
31 :        copy.addDocument(reader);
32 :        //writedump(inFile);

堆栈跟踪错误

java.io.IOException: "C:/ColdFusion10/cfusion/wwwroot/_pdf/YEF5.pdf","C:/ColdFusion10/cfusion/wwwroot/_pdf/1WEX9.pdf","C:/ColdFusion10/cfusion/wwwroot/_pdf/1WFX8.pdf","C:/ColdFusion10/cfusion/wwwroot/_pdf/1WFY6.pdf","C:/ColdFusion10/cfusion/wwwroot/_pdf/1WFZ3.pdf" not found as file or resource.
    at com.lowagie.text.pdf.RandomAccessFileOrArray.<init>(Unknown Source)
    at com.lowagie.text.pdf.RandomAccessFileOrArray.<init>(Unknown Source)
    at com.lowagie.text.pdf.PRTokeniser.<init>(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.<init>(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.<init>(Unknown Source)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at coldfusion.runtime.java.JavaProxy.CreateObject(JavaProxy.java:166)
    at coldfusion.runtime.java.JavaProxy.invoke(JavaProxy.java:80)
    at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2428)
    at cfindex2ecfm33320163.runPage(C:\ColdFusion10\cfusion\wwwroot\index.cfm:30)
    at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:244)
    at coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:446)
    at coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65)
    at coldfusion.filter.IpFilter.invoke(IpFilter.java:64)
    at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:451)
    at coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:48)
    at coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40)
    at coldfusion.filter.PathFilter.invoke(PathFilter.java:112)
    at coldfusion.filter.LicenseFilter.invoke(LicenseFilter.java:30)
    at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:94)
    at coldfusion.filter.BrowserDebugFilter.invoke(BrowserDebugFilter.java:79)
    at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
    at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38)
    at coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:58)
    at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38)
    at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
    at coldfusion.filter.CachingFilter.invoke(CachingFilter.java:62)
    at coldfusion.CfmServlet.service(CfmServlet.java:219)
    at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)
    at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:450)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

如果查看堆栈跟踪消息,则表明提供的路径字符串无效:

  

java.io.IOException的:   “C:/ColdFusion10/cfusion/wwwroot/_pdf/YEF5.pdf”, “C:/ColdFusion10/cfusion/wwwroot/_pdf/1WEX9.pdf”, “C:/ColdFusion10/cfusion/wwwroot/_pdf/1WFX8.pdf” “C:/ColdFusion10/cfusion/wwwroot/_pdf/1WFY6.pdf”, “C:/ColdFusion10/cfusion/wwwroot/_pdf/1WFZ3.pdf”   未找到文件或资源

从错误消息中的值开始,当传入单个路径时,您将传入包含多个文件路径的字符串。如果convertArray变量包含路径列表,请使用ListToArray函数将其转换为数组。只要每个元素包含单个(有效)文件路径,代码就可以正常工作。

如果您仍然收到“找不到文件或资源”错误,则表示路径无效或无法访问。添加一些调试,以便您可以查看实际的文件路径并验证它们是否是有效文件:

<cfscript>
    // convert list of files into array
    convertArray = "c:/path/FirstFile.pdf,c:/otherpath/SecondFile.pdf";
    inFiles = listToArray(convertArray);
    outFile = "c:\temp\merged_1.pdf";

    try {
        // ... 
        for (inFile in inFiles) {

            // Verify current value is valid file path
            writeOutput("<br>DEBUG: Current Path: "& inFile );
            writeOutput(" FileExists("& FileExists(inFile) &")");

            // ... initialize reader and copy file 
        }
    }
    // ...etcetera
</cfscript>