Boa noite pessoal,
Estou com um problema em abrir um relatório que desenvolvi utilizando o JasperReport. Na minha maquina local, abre normalmente, mas quando faço o deploy para o servidor, o mesmo da o seguinte erro quando tento abrir:
java.lang.NullPointerException
java.io.File.<init>(Unknown Source)
br.com.admcw.servlet.ServletApuracao.service(ServletApuracao.java:92)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Pelo que vi, é alguma coisa relacionado a caminho, mas o meu código aparentimente está ok, pois na maquina local ele abre. Segue o meu código para abrir o relatório:
package br.com.admcw.servlet;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.ImageIcon;
import com.lowagie.text.pdf.codec.Base64.OutputStream;
import com.sun.org.apache.xerces.internal.util.URI;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
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.JasperRunManager;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;
@SuppressWarnings("serial")
public class ServletApuracao extends HttpServlet{
private ServletContext sc;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
sc = config.getServletContext();
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ResultSet rs = null;
String retorno = null;
String rel = null;
File reportFile = null;
File reportLogo = null;
String regini = req.getParameter("regini");
String regfim = req.getParameter("regfim");
String pilarini = req.getParameter("pilarini");
String pilarfim = req.getParameter("pilarfim");
String faseini = req.getParameter("faseini");
String fasefim = req.getParameter("fasefim");
String arquivo = req.getParameter("arquivo");
ExecSql execsql = new ExecSql();
try {
rs = execsql.relapuracao(regini, regfim, pilarini, pilarfim, faseini, fasefim );
} catch (SQLException e) {
e.printStackTrace();
}
JRResultSetDataSource jrRS = new JRResultSetDataSource(rs);
Map parameters = new HashMap();
//ImageIcon gto = new ImageIcon(sc.getRealPath("WEB-INF/reports/logowcm.jpg"));
//parameters.put("logo", gto.getImage());
parameters.put("regini", regini);
parameters.put("regfim", regfim);
parameters.put("pilarini", pilarini);
parameters.put("pilarfim", pilarfim);
parameters.put("faseini", faseini);
parameters.put("fasefim", fasefim);
reportFile = new File(sc.getRealPath("WEB-INF/reports/relapuracao.jasper"));
//Exporta para EXCEL
if (arquivo.equals("0")){
JasperPrint impressao = null;
try {
impressao = JasperFillManager.fillReport( reportFile.getPath() , parameters, jrRS );
} catch (JRException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JRXlsExporter exporter = new JRXlsExporter();
ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();
exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, impressao);
exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, xlsReport);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.OUTPUT_FILE_NAME, "c:/temp/relapuracao.xls");
exporter.setParameter(JRXlsExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
try {
exporter.exportReport();
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] bytes = xlsReport.toByteArray();
resp.setContentType("application/vnd.ms-excel");
resp.setContentLength(bytes.length);
xlsReport.close();
ServletOutputStream ouputStream = resp.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
}else{
//Exporta para PDF
byte[] bytes = null;
try {
bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,jrRS);
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (bytes != null && bytes.length > 0) {
resp.setContentType("application/pdf");
resp.setContentLength(bytes.length);
ServletOutputStream ouputStream = resp
.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
}
}
}
@Override
public void destroy() {
}
}
Alguém poderia me ajudar?
Obrigado