net.sf.jasperreports.engine.JRException:java.net.MalformedURLException

时间:2018-11-01 07:56:04

标签: java

使用Jasper Reports为正在处理的Java项目生成收据是我的新手,每次尝试运行代码时都会收到此错误。

net.sf.jasperreports.engine.JRException: java.net.MalformedURLException

我从桌面上的目录获取jrxml文件,其路径位于配置文件中,但错误仍然存​​在。我该怎么办,请帮忙?

这是我的代码:

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

public class DashboardAction extends ActionSupport {

    ResourceBundle resourceBundle = ResourceBundle.getBundle("auth");
    public String reportPath = resourceBundle.getString("ReportPath");

    Logger logger = Logger.getLogger(OrderAction.class);
    String result;
    JasperDesign jasperDesign = null;

    public String printReceipt() {
        logger.info("Inside DashboardAction printReceipt");

        try {

            //Compile the report
            InputStream is = DashboardAction.class.getResourceAsStream(reportPath+"Test.jrxml");
            jasperDesign = JRXmlLoader.load(is);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            JRDataSource datasource = new JREmptyDataSource();

            Map<String, Object> parameters = new HashMap<String, Object>();
            parameters.put("name", "Antony");
            parameters.put("number", "0729455784");

            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, datasource);

            //Export the report
            JasperExportManager.exportReportToPdfFile(jasperPrint, reportPath+"Test_pdf.pdf");
            result = "Success";
        }catch(Exception ex) {
            ex.printStackTrace();
            result = "Fail";
        }

        return result;
    }
}

0 个答案:

没有答案