当在Java Elastic Beanstalk上使用Java Web应用程序时,是否可以使用Java Jasper API直接使用jrxml文件生成pdf。这里我将“empprofile.jrxml”文件存储在“WEB-INF / reports”目录中。在localhost上部署应用程序时,报告成功生成。但是,当部署在AWS Elastic Beanstalk上时,它不会生成pdf。请提供任何解决方案以解决此问题。 下面是我用来生成pdf的代码:
package controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import bean.AppointmentBean;
import bean.EducationBean;
import bean.EmployeeAddressBean;
import bean.EmployeeBankBean;
import bean.EmployeeBean;
import bean.EmployeePersonalBean;
import bean.KnEmployeeBean;
import bean.KrudnyataListBean;
import bean.PayscaleBean;
import bean.RelativeBean;
import bean.ServiceHistoryBean;
import dao.AppointmentDao;
import dao.EducationalDao;
import dao.EmployeeDao;
import dao.PayscaleDao;
import dao.RelativeDao;
import dao.krudnyatanidhi.KrudnyataNidhiDao;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
public class PrintProfile extends HttpServlet {
private static final long serialVersionUID = 1L;
public PrintProfile() {
super();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession(false);
try{
if(session.getAttribute("category")!=null)
{
if(session.getAttribute("category").equals("admin")||session.getAttribute("category").equals("badmin")){
try{
String uid=request.getParameter("uid");
EmployeeDao dao=new EmployeeDao();
EmployeeBean bean=dao.getEmployeeBeanByUID(uid);
request.setAttribute("bean", bean);
session.setAttribute("uid", uid);
request.getRequestDispatcher("printprofile.jsp").forward(request, response);
}catch (Exception e) {
e.printStackTrace();
}
}
else{
session=request.getSession(true);
session.setAttribute("category", null);
response.sendRedirect("../index.jsp?success=0");
}
}
else{
session=request.getSession(true);
session.setAttribute("category", null);
response.sendRedirect("../index.jsp?success=0");
}
}catch(Exception e){
session=request.getSession(true);
session.setAttribute("category", null);
response.sendRedirect("../index.jsp?success=0");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession(false);
try{
if(session.getAttribute("category")!=null)
{
if(session.getAttribute("category").equals("admin")||session.getAttribute("category").equals("badmin")){
try{
String uid=request.getParameter("uid");
String personal=request.getParameter("personal");
String address=request.getParameter("address");
String bank=request.getParameter("bank");
String appt=request.getParameter("appt");
String educational=request.getParameter("educational");
String serviceh=request.getParameter("serviceh");
String payscale=request.getParameter("payscale");
String krudnyatanidhi=request.getParameter("krudnyatanidhi");
String relative=request.getParameter("relative");
String date=request.getParameter("today");
EmployeePersonalBean pbean=null;
EmployeeBankBean empbank=null;
EmployeeAddressBean empaddress=null;
AppointmentBean empappt=null;
ArrayList<EducationBean> qualsds=null;
ArrayList<PayscaleBean> archives=null;
ArrayList<RelativeBean> relatives=null;
ArrayList<ServiceHistoryBean> sh=null;
KrudnyataListBean knstatus=null;
ArrayList<KnEmployeeBean> knlist=new ArrayList<>();
EmployeeDao dao=new EmployeeDao();
EmployeeBean bean=dao.getEmployeeBeanByUID(uid);
if(personal!=null){
pbean=dao.getEmpPersonal(uid);
}
if(bank!=null){
empbank=dao.getEmpBank(uid);
}
if(address!=null){
empaddress=dao.getEmpAddress(uid);
}
InputStream photo=dao.getEmployeePhoto(uid);
if(photo==null){
String directoryPath=getServletContext().getRealPath("images");
File image=new File(directoryPath+File.separator+"pna.png");
photo=new FileInputStream(image);
}
AppointmentDao adao=new AppointmentDao();
empappt=adao.getAppointmentDetailsForReport(uid);
EducationalDao eDao=new EducationalDao();
if(educational!=null){
qualsds=eDao.getEducationalDetails(uid);
}
JRBeanCollectionDataSource quals=new JRBeanCollectionDataSource(qualsds);
PayscaleDao pdao=new PayscaleDao();
if(payscale!=null){
archives=pdao.getPayscaleArchives(uid);
}
JRBeanCollectionDataSource payscalearchive=new JRBeanCollectionDataSource(archives);
RelativeDao rDao=new RelativeDao();
if(relative!=null){
relatives=rDao.getRelativesDetails(uid);
}
JRBeanCollectionDataSource relativeds=new JRBeanCollectionDataSource(relatives);
if(serviceh!=null){
sh=adao.getBranchHReport(uid);
}
JRBeanCollectionDataSource servicehistoryds=new JRBeanCollectionDataSource(sh);
KrudnyataNidhiDao kdao=new KrudnyataNidhiDao();
Double totalpaid=0.0;
if(krudnyatanidhi!=null){
knlist=kdao.getKNListEmployeewiseForReport(uid);
/*for (KrudnyataPaymentBean b: paymenthistory) {
totalpaid=totalpaid+b.getAmount();
}*/
}
JRBeanCollectionDataSource krpds=new JRBeanCollectionDataSource(knlist);
//Report Generation Code
String imgContext=this.getServletContext().getRealPath("/");
Map<String,Object> parameterMap=new HashMap<String,Object>();
parameterMap.put("uid",uid);
parameterMap.put("context",imgContext);
parameterMap.put("personal",personal);
parameterMap.put("address",address);
parameterMap.put("bank",bank);
parameterMap.put("appt",appt);
parameterMap.put("educational",educational);
parameterMap.put("serviceh",serviceh);
parameterMap.put("payscale",payscale);
parameterMap.put("krudnyatanidhi",krudnyatanidhi);
parameterMap.put("relative",relative);
parameterMap.put("bean",bean);
parameterMap.put("pbean",pbean);
parameterMap.put("empbank",empbank);
parameterMap.put("empaddress",empaddress);
parameterMap.put("photo",photo);
parameterMap.put("empappt",empappt);
parameterMap.put("date",date);
parameterMap.put("quals",quals);
parameterMap.put("payscalearchive",payscalearchive);
parameterMap.put("relativeds",relativeds);
parameterMap.put("knstatus",knstatus);
parameterMap.put("krpds",krpds);
parameterMap.put("totalpaid",totalpaid);
parameterMap.put("servicehistoryds",servicehistoryds);
OutputStream outputStream=response.getOutputStream();
response.setContentType("application/pdf");
/*Connection con=DBConnection.createconnection();*/
ServletContext context = getServletContext();
String report = context.getRealPath("/WEB-INF/reports/empprofile.jrxml");
JasperReport jasperReport = null;
JasperDesign jasperDesign = null;
try {
jasperDesign = JRXmlLoader.load(report);
jasperReport = JasperCompileManager.compileReport(report);
byte[] byteStream = JasperRunManager.runReportToPdf(jasperReport,parameterMap,new JREmptyDataSource());
OutputStream outStream = response.getOutputStream();
response.setContentType("application/pdf");
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"",bean.getUid()+"-"+bean.getFname()+" "+bean.getLname()+".pdf");
response.setHeader(headerKey, headerValue);
response.setContentLength(byteStream.length);
outStream.write(byteStream,0,byteStream.length);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}catch (Exception e) {
e.printStackTrace();
}
}
else{
session=request.getSession(true);
session.setAttribute("category", null);
response.sendRedirect("../index.jsp?success=0");
}
}
else{
session=request.getSession(true);
session.setAttribute("category", null);
response.sendRedirect("../index.jsp?success=0");
}
}catch(Exception e){
session=request.getSession(true);
session.setAttribute("category", null);
response.sendRedirect("../index.jsp?success=0");
}
}
}
这是catalina.out文件的AWS日志的一部分:
net.sf.jasperreports.engine.JRException: Byte data not found at : /var/lib/tomcat8/webapps/ROOT/WEB-INF\reports\pdealogo.png
at net.sf.jasperreports.repo.RepositoryUtil.getBytes(RepositoryUtil.java:206)
at net.sf.jasperreports.engine.JRImageRenderer.getInstance(JRImageRenderer.java:141)
at net.sf.jasperreports.engine.fill.JRFillImage.evaluateImage(JRFillImage.java:498)
at net.sf.jasperreports.engine.fill.JRFillImage.evaluate(JRFillImage.java:441)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:257)
at net.sf.jasperreports.engine.fill.JRFillFrame.evaluate(JRFillFrame.java:147)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:257)
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:468)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillTitle(JRVerticalFiller.java:327)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:263)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:129)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:903)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:832)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:84)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:624)
at net.sf.jasperreports.engine.JasperRunManager.runReportToPdf(JasperRunManager.java:421)
at controller.PrintProfile.doPost(PrintProfile.java:236)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
答案 0 :(得分:0)
我有解决方案。我已经从AWS服务器日志检查了catalina.out日志文件。它包含发生的异常,如下所示:
net.sf.jasperreports.engine.JRException: Byte data not found at : /var/lib/tomcat8/webapps/ROOT/WEB-INF\reports\pdealogo.png
at net.sf.jasperreports.repo.RepositoryUtil.getBytes(RepositoryUtil.java:206)
at net.sf.jasperreports.engine.JRImageRenderer.getInstance(JRImageRenderer.java:141)
at net.sf.jasperreports.engine.fill.JRFillImage.evaluateImage(JRFillImage.java:498)
at net.sf.jasperreports.engine.fill.JRFillImage.evaluate(JRFillImage.java:441)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:257)
at net.sf.jasperreports.engine.fill.JRFillFrame.evaluate(JRFillFrame.java:147)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:257)
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:468)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillTitle(JRVerticalFiller.java:327)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:263)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:129)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:903)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:832)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:84)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:624)
at net.sf.jasperreports.engine.JasperRunManager.runReportToPdf(JasperRunManager.java:421)
at controller.PrintProfile.doPost(PrintProfile.java:236)
所以问题是我在Windows上开发我的Web应用程序并将其部署在Linux服务器上,所以基本上它是为linux系统编写路径的问题。我在jrxml文件中更改了Image Path,如下所示:
旧路径:
<imageExpression>
<![CDATA[$P{context}.toString()+"WEB-INF\\reports\\pdealogo.png"]]>
</imageExpression>
新途径:
<imageExpression>
<![CDATA[$P{context}.toString()+"WEB-INF/reports/pdealogo.png"]]>
</imageExpression>