Advertisement
ballchaichana

UploadKeyServlet

Aug 20th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.27 KB | None | 0 0
  1. package th.in.oneauth.servlet;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.nio.file.Files;
  9. import java.nio.file.Path;
  10. import java.nio.file.Paths;
  11.  
  12. import javax.servlet.ServletException;
  13. import javax.servlet.annotation.MultipartConfig;
  14. import javax.servlet.annotation.WebServlet;
  15. import javax.servlet.http.HttpServlet;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;
  18. import javax.servlet.http.Part;
  19.  
  20. import org.apache.commons.io.IOUtils;
  21.  
  22. import th.in.oneauthen.object.SignatureProfileDB;
  23. import th.in.oneauthen.object.UserUidDB;
  24. import th.in.oneauthen.object.DAO.SignatureProfileDAO;
  25.  
  26. import th.in.oneauthen.signing.SignatureProfile;;
  27. @WebServlet("/uploadKey")
  28. @MultipartConfig
  29. public class UploadKeyServlet extends HttpServlet {
  30.    
  31.     /**
  32.      *
  33.      */
  34.     private static final long serialVersionUID = 1L;
  35.  
  36.  
  37. //  public static final String SESSION_PARAM_USER = "userSession";
  38.  
  39.  
  40.     public static final String URL_DASHBORD = "dashboard.jsp";
  41.     public static final String SYSTEM_SIGNATURE_PROFILE = "keyname";
  42.     public static final String KEY_PIN = "password";
  43.     public static final String SIGING_IMG = "img";
  44.    
  45.     public static String SESSION_IMG_KEY = "";
  46.     public static String SESSION_KEY = "";
  47.    
  48.     public static String PATH_KEY ="C:\\userKey\\key\\";
  49.     public static String PATH_IMG_SIGN ="C:\\userKey\\img_sign\\";
  50.    
  51.     public UploadKeyServlet() {
  52.         super();
  53.         // TODO Auto-generated constructor stub
  54.     }
  55.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  56.             throws ServletException, IOException {
  57.         // TODO Auto-generated method stub
  58.         doPost(request, response);
  59.     }
  60.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  61.             throws ServletException, IOException {
  62.        
  63.         //user_uid
  64.         UserUidDB userID = (UserUidDB) request.getSession().getAttribute(LoginServlet.SESSION_PARAM_USER);
  65.         int user_id =userID.getUserId();
  66.        
  67.         // profileName
  68.         String profileName = request.getParameter("profileName"); // Retrieves <input type="text" name="description">
  69.         // password or key_pin
  70.         String password = request.getParameter("key_pin");
  71.        
  72.        
  73.         // save KEY
  74.         Part filePart = request.getPart("file_key"); // Retrieves <input type="file" name="file">
  75.         String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
  76.         InputStream fileContent = filePart.getInputStream();
  77.         byte[] bytes_Key = IOUtils.toByteArray(fileContent);
  78. //      request.getSession().setAttribute("key_PDF", bytes_Key);
  79.  
  80.         String full_PATH = PATH_KEY + fileName;
  81.         try {
  82.             writeBytesToFileNio(bytes_Key, full_PATH);
  83. //          String save_KEY_PATH = saveTo(fileContent, full_PATH);
  84. //          request.getSession().setAttribute(SESSION_KEY, save_KEY_PATH);
  85.         } catch (Exception e) {
  86.             e.printStackTrace();
  87.             LogoutServlet.doLogout(request, response, "System error !! Can not save KEY in srver");
  88.         }
  89.         String save_KEY_PATH = (String) request.getSession().getAttribute(SESSION_KEY);
  90.        
  91.        // save IMG
  92.         Part filePart_img = request.getPart("file_img");
  93.         String fileName_img = Paths.get(filePart_img.getSubmittedFileName()).getFileName().toString();
  94.         InputStream fileContent_img = filePart_img.getInputStream();
  95.         byte[] bytes_Key_img = IOUtils.toByteArray(fileContent_img);
  96.            
  97.         String full_PATH_img = PATH_IMG_SIGN + fileName_img ;  
  98.            try {
  99.                writeBytesToFileNio(bytes_Key_img, full_PATH_img);
  100. //              String save_IMG_PATH = saveTo(fileContent_img, full_PATH_img);
  101. //              request.getSession().setAttribute(SESSION_IMG_KEY, save_IMG_PATH);
  102.                }catch (Exception e) {
  103.                     e.printStackTrace();
  104.                     LogoutServlet.doLogout(request, response, "System error !! Can not save IMG in server");
  105.                }
  106.         String save_IMG_PATH = (String) request.getSession().getAttribute(SESSION_IMG_KEY);
  107.        
  108.        
  109.         //send to check at
  110.         try {
  111.        
  112.         SignatureProfile sessionUploadKey = null;
  113.         sessionUploadKey.generateNewProfile( profileName, bytes_Key, password,  user_id);
  114.         } catch (Exception e) {
  115.             e.printStackTrace();
  116.             LogoutServlet.doLogout(request, response, "GenerateNewProfile Error !" + e.getMessage());
  117.         }
  118.        
  119.         SignatureProfileDAO profileDAO = new SignatureProfileDAO();
  120.         SignatureProfileDB profileDB = new SignatureProfileDB();
  121.             profileDB.setProfileName(profileName);
  122.             profileDB.setProfileKey(full_PATH);
  123.             profileDB.setSigImg(full_PATH_img);
  124.             profileDB.setProfileKeyPIN(password);
  125.             //ΰΈ₯ΰΈ·ΰΈ‘set user uuid
  126.             profileDB.setUserUid(userID);
  127.             try {
  128.                 profileDAO.save(profileDB);
  129.                 System.out.println("Save Key success!!");
  130.                 request.getRequestDispatcher(URL_DASHBORD).forward(request, response);
  131.             } catch (Exception e) {
  132.                 // TODO Auto-generated catch block
  133.                 e.printStackTrace();
  134.                 LogoutServlet.doLogout(request, response, "Error can not Save Key in DB!!" + e.getMessage());
  135.             }
  136.        
  137.        
  138.        
  139.        
  140.     }
  141.    
  142.     private static void writeBytesToFileNio(byte[] bFile, String fileDest) {
  143.  
  144.         try {
  145.             Path path = Paths.get(fileDest);
  146.             Files.write(path, bFile);
  147.         } catch (IOException e) {
  148.             e.printStackTrace();
  149.         }
  150.  
  151.     }
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement