Bom dia a todos !
Estou com um problema, eu tenho contrato feito em Ireport, que ele é gerado no formato pdf, e atravez do outPutStream eu queria apresentar o pdf para o usuario sem ele existir fisicamente, e depois que ele aceita-se o contrato eu guardaria esse contrato no banco de dados.
O meu problema é que atravez do OutputStream eu não estou conseguindo apresentar o arquivo na pagina, se alguem poder me ajudar agradeço.
Abraços (Segue o codigo abaixo da servlet).
package br.com.reports;
import java.io.*;
import java.math.BigDecimal;
import javax.servlet.*;
import javax.servlet.http.*;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.util.JRProperties;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.*;
/*
* @author Weverson Aparecido Taxoto
*/
public class Report_PDF extends HttpServlet {
static final long serialVersionUID = 1L;
int pSize;
Connection conn;
HashMap map;
String repSource, pValue, pName, pType, repName, path, rgm_alun;
private Connection getConnection() {
try {
//cria a conexçao com o banco de dados
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = //Conexão com o banco
} catch (SQLException | ClassNotFoundException ex) {
System.err.println("Error:" + ex);
}
return conn;
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
try {
request.setCharacterEncoding("UTF-8");
JRProperties.setProperty("net.sf.jasperreports.awt.ignore.missing.font", true);
repSource = request.getParameter("report");
path = request.getParameter("path");
pSize = Integer.parseInt(request.getParameter("psize"));
conn = getConnection();
map = new HashMap();
for (int i = 0; i <= pSize; i++) {
pName = request.getParameter("pname_" + i);
pValue = request.getParameter("pvalue_" + i);
pType = request.getParameter("ptype_" + i);
if ("S".equalsIgnoreCase(pType)) {
map.put(pName, pValue);
} else if ("I".equalsIgnoreCase(pType)) {
map.put(pName, new Integer(pValue));
} else if ("D".equalsIgnoreCase(pType)) {
map.put(pName, new Double(pValue));
} else if ("B".equalsIgnoreCase(pType)) {
map.put(pName, new BigDecimal(pValue));
} else if ("F".equalsIgnoreCase(pType)) {
map.put(pName, new Float(pValue));
} else if ("O".equalsIgnoreCase(pType)) {
map.put(pName, (Object) pValue);
}
}
File dir = new File(path);
if (!dir.exists()) dir.mkdirs();
File file = new File(dir + "/rematricula.pdf");
InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream(repSource);
OutputStream out = new FileOutputStream(file);
JasperRunManager.runReportToPdfStream(reportStream, out, map, conn); // modificação do OutputStream
/* int read = 0;
byte[] bytes = new byte[1024];
while ((read = reportStream.read(bytes)) != -1)
out.write(bytes, 0, read);
reportStream.close();
out.flush();
out.close();
*/
byte[] bytes = fileToByte(reportStream, out);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
if (conn != null) conn.close();
} catch (Exception ex) {//Generalizando por causa dos muitos Exceptions
System.err.println("Erro: "+ ex.getMessage());
}
}
public byte[] fileToByte(InputStream reportStream, OutputStream out) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int read = 0;
byte[] bytes = new byte[8192];
while ((read = reportStream.read(bytes, 0, 8192)) != -1)
out.write(bytes, 0, read);
baos.writeTo(out);
return baos.toByteArray();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
}