SHARE
TWEET

Untitled

a guest Aug 14th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package servlet;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.sql.Connection;
  7. import java.util.HashMap;
  8.  
  9. import javax.servlet.ServletContext;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.ServletOutputStream;
  12. import javax.servlet.annotation.WebServlet;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16.  
  17. import connection.SingleConnection;
  18. import net.sf.jasperreports.engine.JRException;
  19. import net.sf.jasperreports.engine.JasperReport;
  20. import net.sf.jasperreports.engine.JasperRunManager;
  21. import net.sf.jasperreports.engine.util.JRLoader;
  22.  
  23. @WebServlet("/pages/relatorio")
  24. public class RelatorioServlet extends HttpServlet {
  25.     private static final long serialVersionUID = 1L;
  26.  
  27.     private Connection connection;
  28.  
  29.     public RelatorioServlet() {
  30.         super();
  31.         this.connection = SingleConnection.getConnection();
  32.     }
  33.  
  34.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  35.             throws ServletException, IOException {
  36.         String modo = request.getParameter("modo");
  37.         //String tipo = request.getParameter("tipo");
  38.         geraRelatorio(request, response, modo);
  39.     }
  40.  
  41.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  42.             throws ServletException, IOException {
  43.     }
  44.  
  45.     private void geraRelatorio(HttpServletRequest request, HttpServletResponse response, String modo) throws ServletException, IOException {
  46.         String jasper = "/WEB-INF/report/Carros.jasper";
  47.         ServletContext contexto = getServletContext();
  48.         HashMap<String, Object> param = new HashMap<String, Object>();
  49.  
  50.         try (ServletOutputStream sos = response.getOutputStream()) {
  51.            
  52.             JasperReport relatorio = (JasperReport) JRLoader.loadObjectFromFile(contexto.getRealPath(jasper));
  53.             byte[] bytes = JasperRunManager.runReportToPdf(relatorio, param, connection);
  54.  
  55.             if (bytes != null) {
  56.                 response.setContentType("application/pdf");
  57.                 response.setContentLength(bytes.length);
  58.                 if (modo.equalsIgnoreCase("download")) {
  59.                     response.setHeader("Content-Disposition", "attachment;filename=relatorio.pdf");
  60.                     downloadFile(bytes, sos);
  61.                 } else if (modo.equalsIgnoreCase("visualizar")) {
  62.                     sos.write(bytes);
  63.                     sos.flush();
  64.                 }
  65.             }
  66.  
  67.         } catch (JRException e) {
  68.             e.printStackTrace();
  69.         }
  70.  
  71.     }
  72.  
  73.     private static void downloadFile(byte[] bytes, ServletOutputStream sos) {
  74.         try (InputStream i = new ByteArrayInputStream(bytes)) {
  75.             int bytesRead = i.read();
  76.  
  77.             while (bytesRead != -1) {
  78.                 sos.write(bytesRead);
  79.                 bytesRead = i.read();
  80.             }
  81.         } catch (Exception e) {
  82.             e.printStackTrace();
  83.         }
  84.  
  85.     }
  86.  
  87. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top