JasperPrint FileNotFoundException 但文件显示错误

JasperPrint FileNotFoundException but file IS shown on error

我正在尝试打印一份 jasper 报告,但它一直给我一个 FileNotFoundException 错误。

一点上下文,设计这个的人在存储库之外有 jrxml 文件,使用这个代码(它有效,它实际上打印它)

@Value("/app/reportes/sale_ticket.jrxml")
private String sourceFileName;

String report = JasperCompileManager.compileReportToFile(sourceFileName);
parameters.put("movement_id", saleId);
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters);
/*Do more stuff here*/

我被要求将文件移动到存储库中,以便我们可以获得它的实际版本,所以我做到了,我把它放在 src/main/reports 下,我们这里有很多其他文件,但我们没有实际上打印它们,我们将它们导出为 PDF 并且它工作正常。我的打印代码如下:

InputStream jrxml = this.getClass().getClassLoader().getResourceAsStream("reports/sale_ticket.jrxml");

String convertInputStreamToString = new BufferedReader(new InputStreamReader(jrxml, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
parameters.put("movementId", movementId);
JasperPrint jasperPrint = JasperFillManager.fillReport(convertInputStreamToString, parameters);

我在 JasperPrint 行遇到异常,我不知道为什么它在异常中实际列出整个 jrxml 文件时说 FileNotFound:

22-06-2020 13:38:04.207 [http-nio-8080-exec-1] ERROR mx.ssf.sicom.services.PrinterService.printTicket - 
net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: <?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.12.2.final using JasperReports Library version 6.12.2-75c5e90a222ab406e416cbf590a5397028a52de3  -->
<jasperReport xmlns="http:/jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http:/www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:/jasperreports.sourceforge.net/jasperreports http:/jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="saleTicketReport" pageWidth="226" pageHeight="400" columnWidth="190" leftMargin="14" rightMargin="22" topMargin="14" bottomMargin="14" uuid="0fa1be9a-5b01-4c84-9407-c4df58051f37">
        <property name="com.jaspersoft.studio.unit." value="mm"/>
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
        <subDataset name="saleTicketReportDataSet" uuid="ada8590b-f059-41ce-88cb-29db840877c4">
                <property name="com.jaspersoft.studio.data.defaultdataadapter" value="ConsumableAndMaterialBookBeanReport"/>
                <queryString>
                        <![CDATA[]]>
                </queryString>
                <field name="street" class="java.lang.String">
                        <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="number" class="java.lang.String"/>
                <field name="colony" class="java.lang.String"/>
                <field name="municipality" class="java.lang.String"/>
                <field name="postalCode" class="java.lang.String"/>
                <field name="state" class="java.lang.String"/>
                <field name="folio" class="java.lang.String"/>
                <field name="hoseKey" class="java.lang.String"/>
                <field name="initialDate" class="java.sql.Timestamp"/>
                <field name="fecha" class="java.sql.Timestamp"/>
                <field name="price" class="java.math.BigDecimal">
                        <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
                <field name="amount" class="java.math.BigDecimal">
                        <fieldDescription><![CDATA[]]></fieldDescription>
                </field>
        </subDataset>
        <parameter name="movementId" class="java.lang.String">
                <parameterDescription><![CDATA[]]></parameterDescription>
        </parameter>
        <parameter name="street" class="java.lang.String">
                <parameterDescription><![CDATA[]]></parameterDescription>
        </parameter>
        <parameter name="number" class="java.lang.String">
                <parameterDescription><![CDATA[]]></parameterDescription>
        </parameter>
        <parameter name="colony" class="java.lang.String">
                <parameterDescription><![CDATA[]]></parameterDescription>
        </parameter>
        <parameter name="municipality" class="java.lang.String">
                <parameterDescription><![CDATA[]]></parameterDescription>
        </parameter>
        <parameter name="postalCode" class="java.lang.String">
                <parameterDescription><![CDATA[]]></parameterDescription>
        </parameter>
        <parameter name="state" class="java.lang.String">
                <parameterDescription><![CDATA[]]></parameterDescription>
        </parameter>
        <parameter name="folio" class="java.lang.String">
                <parameterDescription><![CDATA[]]></parameterDescription>
        </parameter>
        <parameter name="hoseKey" class="java.lang.String">
                <parameterDescription><![CDATA[]]></parameterDescription>
        </parameter>
        <parameter name="initialDate" class="java.sql.Timestamp">
                <parameterDescription><![CDATA[]]></parameterDescription>
        </parameter>
        <parameter name="printDate" class="java.sql.Timestamp">
                <parameterDescription><![CDATA[]]></parameterDescription>
        </parameter>
        <parameter name="price" class="java.math.BigDecimal">
                <parameterDescription><![CDATA[]]></parameterDescription>
        </parameter>
        <parameter name="amount" class="java.math.BigDecimal">
                <parameterDescription><![CDATA[]]></parameterDescription>
        </parameter>
        <parameter name="equipmentKey" class="java.lang.String">
                <parameterDescription><![CDATA[]]></parameterDescription>
        </parameter>
        <parameter name="volume" class="java.math.BigDecimal">
                <parameterDescription><![CDATA[]]></parameterDescription>
        </parameter>
        <parameter name="name" class="java.lang.String">
                <parameterDescription><![CDATA[]]></parameterDescription>
        </parameter>
        <title>
                <band height="102" splitType="Stretch">
                        <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                        <textField>
                                <reportElement x="0" y="0" width="190" height="51" uuid="224d03ff-4ce2-4cd1-858c-a9b145ef18c3"/>
                                <textElement textAlignment="Center">
                                        <font size="16"/>
                                </textElement>
                                <textFieldExpression><![CDATA[$P{name}]]></textFieldExpression>
                        </textField>
                        <textField>
                                <reportElement x="0" y="51" width="190" height="51" uuid="14ea115a-c175-475a-9ecb-1f36b7a06285"/>
                                <textElement textAlignment="Center">
                                        <font size="10"/>
                                </textElement>
                                <textFieldExpression><![CDATA[$P{street} + " " + $P{number} + ", Col. " + $P{colony} + ", " + $P{municipality} + " CP. " + $P{postalCode} + ", " + $P{state}]]></textFieldExpression>
                        </textField>
                </band>
        </title>
        <detail>
                <band height="144" splitType="Stretch">
                        <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
                        <frame>
                                <reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="190" height="30" uuid="ba8962db-88e8-4506-8021-de56c7454a48">
                                        <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                                </reportElement>
                                <textField>
                                        <reportElement x="0" y="0" width="190" height="15" uuid="25ff50b4-4083-42c2-ba82-f7df59ca6a9d"/>
                                        <textElement>
                                                <font isBold="true"/>
                                        </textElement>
                                        <textFieldExpression><![CDATA["FOLIO NOTA : "]]></textFieldExpression>
                                </textField>
                                <textField pattern="#,##0.00#">
                                        <reportElement x="0" y="15" width="190" height="15" uuid="47188078-736b-490f-9bef-fa3d9c1c2029"/>
                                        <textElement textAlignment="Left">
                                                <font size="12" isBold="true"/>
                                        </textElement>
                                        <textFieldExpression><![CDATA[$P{folio}]]></textFieldExpression>
                                </textField>
                        </frame>
                        <frame>
                                <reportElement x="0" y="40" width="190" height="90" uuid="73922b36-4376-491c-b515-117f447e6878">
                                        <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                                </reportElement>
                                <frame>
                                        <reportElement x="0" y="0" width="190" height="12" uuid="5cc6e064-1770-4027-8345-8b30dc345d70">
                                                <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
                                        </reportElement>
                                        <textField>
                                                <reportElement x="0" y="0" width="110" height="12" uuid="6e76daa6-2279-4041-9385-e96e7f9d8349"/>
                                                <textElement>
                                                        <font size="8"/>
                                                </textElement>
                                                <textFieldExpression><![CDATA["DISPENSARIO: "]]></textFieldExpression>
                                        </textField>
                                        <textField>
                                                <reportElement x="110" y="0" width="80" height="12" uuid="62c90970-54fc-44e5-bec0-507809ddfad3"/>
                                                <textElement>
                                                        <font size="8"/>
                                                </textElement>
                                                <textFieldExpression><![CDATA[$P{equipmentKey}]]></textFieldExpression>
                                        </textField>
                                </frame>
                                <frame>
                                        <reportElement x="0" y="12" width="190" height="12" uuid="11667257-3493-4a46-b455-07bfd499127b">
                                                <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
                                        </reportElement>
                                        <textField>
                                                <reportElement x="0" y="0" width="110" height="12" uuid="62e5c0aa-3cfd-4d50-b50a-8b144081f7af"/>
                                                <textElement>
                                                        <font size="8"/>
                                                </textElement>
                                                <textFieldExpression><![CDATA["MANGUERA: "]]></textFieldExpression>
                                        </textField>
                                        <textField>
                                                <reportElement x="110" y="0" width="80" height="12" uuid="a8baf2c4-1297-40cd-ad94-ad32ee7bf835"/>
                                                <textElement>
                                                        <font size="8"/>
                                                </textElement>
                                                <textFieldExpression><![CDATA[$P{hoseKey}]]></textFieldExpression>
                                        </textField>
                                </frame>
                                <frame>
                                        <reportElement x="0" y="24" width="190" height="12" uuid="4c670858-41ff-4497-9702-165355242b13">
                                                <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
                                        </reportElement>
                                        <textField>
                                                <reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="110" height="12" uuid="f49c2fb2-b693-4b3b-b1a7-76a8a7a38f3f">
                                                        <property name="com.jaspersoft.studio.unit.width" value="mm"/>
                                                </reportElement>
                                                <textElement>
                                                        <font size="8"/>
                                                </textElement>
                                                <textFieldExpression><![CDATA["FECHA/HORA SERVICIO:"]]></textFieldExpression>
                                        </textField>
                                        <textField pattern="dd/MM/yy HH:mm:ss">
                                                <reportElement x="110" y="0" width="80" height="12" uuid="8ca466da-f812-4b2c-ab26-8d700d5ee9a8"/>
                                                <textElement>
                                                        <font size="8"/>
                                                </textElement>
                                                <textFieldExpression><![CDATA[$P{initialDate}]]></textFieldExpression>
                                        </textField>
                                </frame>
                                <frame>
                                        <reportElement x="0" y="36" width="190" height="12" uuid="69021f35-3291-4c01-ab1c-ed332c0edd57">
                                                <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
                                        </reportElement>
                                        <textField>
                                                <reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="110" height="12" uuid="38854563-1959-4256-8bbb-4951b043dc36"/>
                                                <textElement>
                                                        <font size="8"/>
                                                </textElement>
                                                <textFieldExpression><![CDATA["FECHA/HORA IMPRESION: "]]></textFieldExpression>
                                        </textField>
                                        <textField pattern="dd/MM/yy HH:mm:ss">
                                                <reportElement x="110" y="0" width="80" height="12" uuid="86ecfd7a-e0f7-41a8-b3ef-448ebb2b06cc"/>
                                                <textElement>
                                                        <font size="8"/>
                                                </textElement>
                                                <textFieldExpression><![CDATA[$P{printDate}]]></textFieldExpression>
                                        </textField>
                                </frame>
                                <frame>
                                        <reportElement x="0" y="48" width="190" height="18" uuid="c0b08637-a50d-4337-85c0-d676bdd99ebc">
                                                <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
                                        </reportElement>
                                        <textField>
                                                <reportElement x="0" y="0" width="110" height="18" uuid="44b49e5e-812d-4600-af33-aa1dbaaf84f9"/>
                                                <textElement>
                                                        <font size="15"/>
                                                </textElement>
                                                <textFieldExpression><![CDATA["VOLUMEN: "]]></textFieldExpression>
                                        </textField>
                                        <textField pattern="#,##0.00#">
                                                <reportElement x="110" y="0" width="80" height="18" uuid="dc96804e-2def-4df6-9ce2-a8b0e8ab77e6"/>
                                                <textElement textAlignment="Left">
                                                        <font size="15" isBold="true"/>
                                                </textElement>
                                                <textFieldExpression><![CDATA[$P{volume}]]></textFieldExpression>
                                        </textField>
                                </frame>
                                <frame>
                                        <reportElement x="0" y="66" width="190" height="12" uuid="826db372-32e2-4fdf-b0d3-686ed64c7a13">
                                                <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.HorizontalRowLayout"/>
                                        </reportElement>
                                        <textField>
                                                <reportElement x="0" y="0" width="95" height="12" uuid="274dec5f-772d-4cd3-aa1e-1117323979ee"/>
                                                <textElement>
                                                        <font size="8"/>
                                                </textElement>
                                                <textFieldExpression><![CDATA["PRECIO UNITARIO: "]]></textFieldExpression>
                                        </textField>
                                        <textField pattern="¤#,##0.00;¤-#,##0.00">
                                                <reportElement x="95" y="0" width="95" height="12" uuid="c25eb36d-f1b1-4a42-bf30-cd5dbb5cf345"/>
                                                <textElement textAlignment="Right">
                                                        <font size="8" isBold="true"/>
                                                </textElement>
                                                <textFieldExpression><![CDATA[$P{price}]]></textFieldExpression>
                                        </textField>
                                </frame>
                                <frame>
                                        <reportElement x="0" y="78" width="190" height="12" uuid="65ed7d8a-204b-48de-9081-80dc5786b66e">
                                                <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.HorizontalRowLayout"/>
                                        </reportElement>
                                        <textField>
                                                <reportElement x="0" y="0" width="95" height="12" uuid="00a53814-23ed-4bc6-ace4-e2d9dcf3c931"/>
                                                <textElement>
                                                        <font size="8"/>
                                                </textElement>
                                                <textFieldExpression><![CDATA["IMPORTE TOTAL: "]]></textFieldExpression>
                                        </textField>
                                        <textField pattern="¤#,##0.00;¤-#,##0.00">
                                                <reportElement x="95" y="0" width="95" height="12" uuid="7ad6980b-b4a6-4199-a230-155c330788fd"/>
                                                <textElement textAlignment="Right">
                                                        <font size="8" isBold="true"/>
                                                </textElement>
                                                <textFieldExpression><![CDATA[$P{amount}]]></textFieldExpression>
                                        </textField>
                                </frame>
                        </frame>
                </band>
        </detail>
        <pageFooter>
                <band height="15" splitType="Stretch">
                        <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                        <property name="com.jaspersoft.studio.unit.height" value="mm"/>
                        <textField>
                                <reportElement x="0" y="0" width="190" height="15" uuid="f8c1f13e-64fb-4c58-b8ec-7667e441ffe2"/>
                                <textElement textAlignment="Center">
                                        <font size="12" isBold="true"/>
                                </textElement>
                                <textFieldExpression><![CDATA["Gracias por su compra."]]></textFieldExpression>
                        </textField>
                </band>
        </pageFooter>
</jasperReport>
        at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:127)
        at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:116)
        at net.sf.jasperreports.engine.JasperFillManager.getReportSource(JasperFillManager.java:1105)
        at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:363)
        at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:862)
        at mx.ssf.sicom.services.PrinterService.printTicket(PrinterService.java:99)

JasperFillManager.fillReport(String, Map) 方法将 String 参数解释为文件名,请参阅 here

如果你想直接传递JRXML内容,你应该使用带有InputStream参数的方法,即this.

所以你需要的是:

InputStream jrxml = this.getClass().getClassLoader().getResourceAsStream("reports/sale_ticket.jrxml");

parameters.put("movementId", movementId);
JasperPrint jasperPrint = JasperFillManager.fillReport(jrxml, parameters);