Advertisement
ballchaichana

new upload

Sep 7th, 2018
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.24 KB | None | 0 0
  1. package th.in.oneauth.servlet;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.nio.file.Files;
  10. import java.nio.file.Path;
  11. import java.nio.file.Paths;
  12.  
  13. import javax.servlet.ServletException;
  14. import javax.servlet.annotation.MultipartConfig;
  15. import javax.servlet.annotation.WebServlet;
  16. import javax.servlet.http.HttpServlet;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import javax.servlet.http.Part;
  20.  
  21. import org.apache.commons.io.IOUtils;
  22.  
  23. import th.in.oneauthen.object.SignatureProfileDB;
  24. import th.in.oneauthen.object.UserUidDB;
  25. import th.in.oneauthen.object.DAO.SignatureProfileDAO;
  26.  
  27. import th.in.oneauthen.signing.SignatureProfile;;
  28.  
  29. @WebServlet("/uploadKey")
  30. @MultipartConfig
  31. public class UploadKeyServlet extends HttpServlet {
  32.  
  33.     /**
  34.      *
  35.      */
  36.     private static final long serialVersionUID = 1L;
  37.  
  38.     // public static final String SESSION_PARAM_USER = "userSession";
  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 static String PARAM_ACTION = "action";
  52.     public static final String PARAM_ACTION_UPLOAD = "upload";
  53.     public static final String PARAM_ACTION_EDIT = "edit";
  54.  
  55.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  56.             throws ServletException, IOException {
  57.         //edit  old profile
  58.         String oldProfileName = request.getParameter("oldprofileName");
  59.        
  60.  
  61.         // user_uid
  62.         UserUidDB userID = (UserUidDB) request.getSession().getAttribute(LoginServlet.SESSION_PARAM_USER);
  63.         int user_id = userID.getUserId();
  64.  
  65.         // profileName
  66.         String profileName = request.getParameter("profileName"); // Retrieves <input type="text" name="description">
  67.         // password or key_pin
  68.         String password = request.getParameter("key_pin");
  69.  
  70.         // save KEY
  71.         Part filePart = request.getPart("file_key"); // Retrieves <input type="file" name="file">
  72.         String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
  73.         InputStream fileContent = filePart.getInputStream();
  74.         byte[] bytes_Key = IOUtils.toByteArray(fileContent);
  75.         // request.getSession().setAttribute("key_PDF", bytes_Key);
  76.  
  77.         String full_PATH = PATH_KEY + fileName;
  78.         try {
  79.             writeBytesToFileNio(bytes_Key, full_PATH);
  80.             // String save_KEY_PATH = saveTo(fileContent, full_PATH);
  81.             // request.getSession().setAttribute(SESSION_KEY, save_KEY_PATH);
  82.         } catch (Exception e) {
  83.             e.printStackTrace();
  84.             LogoutServlet.doLogout(request, response, "System error !! Can not save KEY in srver");
  85.         }
  86.         String save_KEY_PATH = (String) request.getSession().getAttribute(SESSION_KEY);
  87.  
  88.         // save IMG
  89.         Part filePart_img = request.getPart("file_img");
  90.         String fileName_img = Paths.get(filePart_img.getSubmittedFileName()).getFileName().toString();
  91.         InputStream fileContent_img = filePart_img.getInputStream();
  92.         byte[] bytes_Key_img = IOUtils.toByteArray(fileContent_img);
  93.  
  94.         String full_PATH_img = PATH_IMG_SIGN + fileName_img;
  95.         try {
  96.             writeBytesToFileNio(bytes_Key_img, full_PATH_img);
  97.             // String save_IMG_PATH = saveTo(fileContent_img, full_PATH_img);
  98.             // request.getSession().setAttribute(SESSION_IMG_KEY, save_IMG_PATH);
  99.         } catch (Exception e) {
  100.             e.printStackTrace();
  101.             LogoutServlet.doLogout(request, response, "System error !! Can not save IMG in server");
  102.         }
  103.         String save_IMG_PATH = (String) request.getSession().getAttribute(SESSION_IMG_KEY);
  104.  
  105.         // send to check at
  106.         try {
  107.  
  108.             SignatureProfile sessionUploadKey = null;
  109.             sessionUploadKey.generateNewProfile(profileName, bytes_Key, password, user_id);
  110.         } catch (Exception e) {
  111.             e.printStackTrace();
  112.             LogoutServlet.doLogout(request, response, "GenerateNewProfile Error !" + e.getMessage());
  113.         }
  114.         PARAM_ACTION = "upload";
  115.         switch (PARAM_ACTION) {
  116.         case PARAM_ACTION_UPLOAD:
  117.             SignatureProfileDAO profileDAO = new SignatureProfileDAO();
  118.             SignatureProfileDB profileDB = new SignatureProfileDB();
  119.             SignatureProfileDB profileCheck = new SignatureProfileDAO().findByNameAndUserUID(profileName, user_id);
  120.             if(profileCheck!=null) {
  121.                 LogoutServlet.doLogout(request, response, "Error can not Save same profilename");
  122.             }
  123.             else {
  124.                 profileDB.setProfileName(profileName);
  125.                 profileDB.setProfileKey(full_PATH);
  126.                 profileDB.setSigImg(full_PATH_img);
  127.                 profileDB.setProfileKeyPIN(password);
  128.                 profileDB.setSigDisplayPage(1);
  129.                 profileDB.setUserUid(userID);
  130.                 try {
  131.                     profileDAO.save(profileDB);
  132.                     System.out.println("Save Key success!!");
  133.                     request.getRequestDispatcher(URL_DASHBORD).forward(request, response);
  134.                 } catch (Exception e) {
  135.                     // TODO Auto-generated catch block
  136.                     e.printStackTrace();
  137.                     LogoutServlet.doLogout(request, response, "Error can not Save Key in DB!!" + e.getMessage());
  138.                 }
  139.             }
  140.  
  141.             break;
  142.         case PARAM_ACTION_EDIT:
  143.             SignatureProfileDB editSigProfile = new SignatureProfileDAO().findByNameAndUserUID("chaichana555", user_id);//(oldProfileName,user_id);
  144.             SignatureProfileDB checkProfile = new SignatureProfileDAO().findByNameAndUserUID(profileName, user_id);
  145.             if(checkProfile==null) {
  146.                 editSigProfile.setProfileName(profileName);
  147.                 editSigProfile.setProfileKey(full_PATH);
  148.                 editSigProfile.setProfileKeyPIN(password);
  149.                 editSigProfile.setSigImg(full_PATH_img);
  150.                 new SignatureProfileDAO().updateSignatureProfileEdit(editSigProfile);
  151.                 System.out.println("Edit profile success!!");
  152.                 request.getRequestDispatcher(URL_DASHBORD).forward(request, response);
  153.             }else {
  154.                
  155.                 LogoutServlet.doLogout(request, response, "Error can not Edit same profilename");
  156.             }
  157.  
  158.             break;
  159.         }
  160.  
  161.     }
  162.  
  163.     private static void writeBytesToFileNio(byte[] bFile, String fileDest) {
  164.  
  165.         try {
  166.             Path path = Paths.get(fileDest);
  167.             Files.write(path, bFile);
  168.         } catch (IOException e) {
  169.             e.printStackTrace();
  170.         }
  171.  
  172.     }
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement