我试图加载我创建的碧玉报告(.jrxml),我将报告命名为“JREmp1.xml”。但我得到了这个错误
HTTP状态500 -请求处理失败;nested exception is net.sf.jasperreports.engine.JRException:java.io.FileNotFoundException:D:\printpdf.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\JasperExample\jasper\JREmp1.jrxml(系统找不到指定的路径)”
如何得到确切的位置?下面是我的JREmp1.xml文件位置:
下面是我的控制器类中的代码:
@RequestMapping(value = "/generateReport", method = RequestMethod.POST)
public String generateReport(
@Valid @ModelAttribute("jasperInputForm") JasperInputForm jasperInputForm,
BindingResult result, Model model, HttpServletRequest request,
HttpServletResponse response) throws JRException, IOException,
NamingException {
if (result.hasErrors()) {
System.out.println("validation error occured in jasper input form");
return "loadJasper";
}
String reportFileName = "JREmp1";
JasperReportDAO jrdao = new JasperReportDAO();
Connection conn = null;
try {
conn = jrdao.getConnection();
String rptFormat = jasperInputForm.getRptFmt();
String noy = jasperInputForm.getNoofYears();
System.out.println("rpt format " + rptFormat);
System.out.println("no of years " + noy);
HashMap<String, Object> hmParams = new HashMap<String, Object>();
hmParams.put("noy", new Integer(noy));
hmParams.put("Title", "Employees working more than " + noy
+ " Years");
JasperReport jasperReport = jrdao.getCompiledFile(reportFileName,
request);
if (rptFormat.equalsIgnoreCase("html")) {
JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport, hmParams, conn);
jrdao.generateReportHtml(jasperPrint, request, response); // For
// HTML
// report
}
else if (rptFormat.equalsIgnoreCase("pdf")) {
jrdao.generateReportPDF(response, hmParams, jasperReport, conn); // For
// PDF
// report
}
} catch (SQLException sqlExp) {
System.out.println("Exception::" + sqlExp.toString());
} finally {
if (conn != null) {
try {
conn.close();
conn = null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
下面是我的JasperReportDAO
类中的代码:
public JasperReport getCompiledFile(String fileName, HttpServletRequest request) throws JRException {
System.out.println("path " + request.getSession().getServletContext().getRealPath("/jasper/" + fileName + ".jasper"));
File reportFile = new File( request.getSession().getServletContext().getRealPath("/jasper/" + fileName + ".jasper"));
// If compiled file is not found, then compile XML template
if (!reportFile.exists()) {
JasperCompileManager.compileReportToFile(request.getSession().getServletContext().getRealPath("/jasper/" + fileName + ".jrxml"),request.getSession().getServletContext().getRealPath("/jasper/" + fileName + ".jasper"));
}
JasperReport jasperReport = (JasperReport) JRLoader.loadObjectFromFile(reportFile.getPath());
return jasperReport;
}
public void generateReportHtml( JasperPrint jasperPrint, HttpServletRequest req, HttpServletResponse resp) throws IOException, JRException {
HtmlExporter exporter=new HtmlExporter();
List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();
jasperPrintList.add(jasperPrint);
exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
exporter.setExporterOutput( new SimpleHtmlExporterOutput(resp.getWriter()));
SimpleHtmlReportConfiguration configuration =new SimpleHtmlReportConfiguration();
exporter.setConfiguration(configuration);
exporter.exportReport();
}
public void generateReportPDF (HttpServletResponse resp, Map parameters, JasperReport jasperReport, Connection conn)throws JRException, NamingException, SQLException, IOException {
byte[] bytes = null;
bytes = JasperRunManager.runReportToPdf(jasperReport,parameters,conn);
resp.reset();
resp.resetBuffer();
resp.setContentType("application/pdf");
resp.setContentLength(bytes.length);
ServletOutputStream ouputStream = resp.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
}
下面是我的JasperInputForm
类:
public class JasperInputForm {
@NotEmpty
private String noofYears;
private String rptFmt="Html";
public String getRptFmt() {
return rptFmt;
}
public void setRptFmt(String rptFmt) {
this.rptFmt = rptFmt;
}
public String getNoofYears() {
return noofYears;
}
public void setNoofYears(String noofYears) {
this.noofYears = noofYears;
}
}
如何正确获取JREmp1.jrxml
文件位置?我为Spring MVC应用程序开发此报表
更新:这是我用@Wilson answer更新后的完整函数代码(我用@Wilson说的第二个选项):这个函数在JasperReportDAO中:
public JasperReport getCompiledFile(String fileName, HttpServletRequest request) throws JRException, MalformedURLException, URISyntaxException {
System.out.println("path " + request.getSession().getServletContext().getRealPath("/jasper/" + fileName + ".jasper"));
//File reportFile = new File( request.getSession().getServletContext().getRealPath("/jasper/" + fileName + ".jasper"));
URL resourceUrl = request.getSession().getServletContext().getResource("/WEB-INF/jasper/" + fileName + ".jrxml");
File reportFile = new File(resourceUrl.toURI());
// If compiled file is not found, then compile XML template
if (!reportFile.exists()) {
JasperCompileManager.compileReportToFile(request.getSession().getServletContext().getRealPath("/jasper/" + fileName + ".jrxml"),request.getSession().getServletContext().getRealPath("/jasper/" + fileName + ".jasper"));
}
JasperReport jasperReport = (JasperReport) JRLoader.loadObjectFromFile(reportFile.getPath());
return jasperReport;
}
我得到了这个错误
HTTP状态500 -请求处理失败;嵌套异常为java.lang.IllegalArgumentException:URI方案不是“文件”
如何解决这个问题?
3条答案
按热度按时间ndasle7k1#
使用
ServeltContext
读取文件的方法有很多:1.使用
ServletContext#getRealPath
这将为您提供您正在查找的资源的完整系统路径。但是,如果容器不展开WAR文件,它将无法工作。
2.使用
ServletContext#getResource
这将返回URL,无论您使用什么容器以及应用程序安装在何处。
3. User
ServletContext#getResourceAsStream
这是
ServletContext#getResource
的替代方案,可获得inputSteam
更新:
从
ServletContext#getResource
返回的URL可能不是文件URL,这可能会导致问题。请使用JasperCompileManager#compileReportToFile
尝试ServletContext#getResourceAsStream
:我发现你正在尝试将碧玉报告文件写入你的程序发行版,这应该避免。首选的方法是预编译您的报告并将其放入WAR文件,或者将编译后的碧玉报告放入临时目录。
以下是完整的代码示例:
5kgi1eie2#
如果jrxml存在于
Resource
文件夹中,我们可以使用下面的代码片段-xsuvu9jc3#
如果.jrml文件位于资源文件夹中,则此方法有效