ballchaichana

download

Dec 14th, 2018
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package th.in.oneauthen.servlet;
  2.  
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Path;
  6. import java.nio.file.Paths;
  7.  
  8. import javax.servlet.ServletException;
  9. import javax.servlet.annotation.WebServlet;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13.  
  14. import org.apache.commons.lang3.StringUtils;
  15.  
  16. /**
  17.  * Servlet implementation class DownloadServlet
  18.  */
  19. @WebServlet("/DownloadServlet")
  20. public class DownloadServlet extends HttpServlet {
  21.     private static final long serialVersionUID = 1L;
  22.        
  23.     /**
  24.      * @see HttpServlet#HttpServlet()
  25.      */
  26.     public DownloadServlet() {
  27.         super();
  28.         // TODO Auto-generated constructor stub
  29.     }
  30.  
  31.     /**
  32.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  33.      */
  34.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  35.  
  36.         String FileLocation = (String) request.getSession(false).getAttribute(SigningServlet.PARAM_FILE_LOCATION);
  37.         String check  = (String) request.getSession().getAttribute(LoginServlet.CHECK);
  38.         if (check == null){
  39.             check = "0";
  40.         }
  41.  
  42.  
  43.         if(check.equals("1")){
  44.             byte[] signPDF = (byte[]) request.getSession().getAttribute(SigningServlet.PDF_DATA);
  45.             response.setContentType("application/pdf");
  46.             response.addHeader("Content-Disposition", "attachment; filename=signDocument.pdf" );
  47.             response.setContentLength(signPDF.length);
  48.             response.getOutputStream(). write(signPDF);
  49.            
  50.  
  51.            request.getSession().setAttribute(LoginServlet.CHECK, "0");
  52.            
  53.            
  54.            
  55.         }
  56.         else {
  57.              response.sendRedirect("Error.jsp");
  58.         }
  59.  
  60.         if (!StringUtils.isEmpty(FileLocation)){
  61.             String Del = FileLocation;
  62.             Files.delete(Paths.get(Del));  
  63.         }
  64.  
  65.     }
  66.  
  67. //  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  68. //
  69. //          byte[] signPDF = Files.readAllBytes(Paths.get("E:\\BallzaR5R5\\test.pdf"));
  70. //          response.setContentType("application/pdf");
  71. //          response.addHeader("Content-Disposition", "attachment; filename=signDocument.pdf" );
  72. //          response.setContentLength(signPDF.length);
  73. //          response.getOutputStream(). write(signPDF);
  74. //         
  75. //
  76. //         request.getSession().setAttribute(LoginServlet.CHECK, "0");
  77. //
  78. //  }
  79.    
  80.    
  81.     /**
  82.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  83.      */
  84.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  85.         // TODO Auto-generated method stub
  86.         doGet(request, response);
  87.     }
  88.  
  89. }
Add Comment
Please, Sign In to add comment