Advertisement
ballchaichana

uploadkeyy

Aug 9th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.13 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 SYSTEM_SIGNATURE_PROFILE = "keyname";
  41.     public static final String KEY_PIN = "password";
  42.     public static final String SIGING_IMG = "img";
  43.    
  44.     public static String SESSION_IMG_KEY = "";
  45.     public static String SESSION_KEY = "";
  46.    
  47.     public static String PATH_KEY ="C:\\user\\Desktop\\key\\";
  48.     public static String PATH_IMG_SIGN ="C:\\user\\Desktop\\img_sign\\";
  49.    
  50.     public UploadKeyServlet() {
  51.         super();
  52.         // TODO Auto-generated constructor stub
  53.     }
  54.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  55.             throws ServletException, IOException {
  56.         // TODO Auto-generated method stub
  57.         doPost(request, response);
  58.     }
  59.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  60.             throws ServletException, IOException {
  61.        
  62.         //user_uid
  63.         UserUidDB userID = (UserUidDB) request.getSession().getAttribute(LoginServlet.SESSION_PARAM_USER);
  64.         int user_id =userID.getUserId();
  65.        
  66.         // profileName
  67.         String profileName = request.getParameter("description"); // Retrieves <input type="text" name="description">
  68.         // password or key_pin
  69.         String password = request.getParameter("key_pin");
  70.        
  71.        
  72.         // save KEY
  73.         Part filePart = request.getPart("file"); // Retrieves <input type="file" name="file">
  74.         String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
  75.         InputStream fileContent = filePart.getInputStream();
  76.         byte[] bytes_Key = IOUtils.toByteArray(fileContent);
  77. //      request.getSession().setAttribute("key_PDF", bytes_Key);
  78.  
  79.         String full_PATH = PATH_KEY + fileName;
  80.         try {
  81.             writeBytesToFileNio(bytes_Key, full_PATH);
  82. //          String save_KEY_PATH = saveTo(fileContent, full_PATH);
  83. //          request.getSession().setAttribute(SESSION_KEY, save_KEY_PATH);
  84.         } catch (Exception e) {
  85.             e.printStackTrace();
  86.             LogoutServlet.doLogout(request, response, "System error !! Can not save KEY in srver");
  87.         }
  88.         String save_KEY_PATH = (String) request.getSession().getAttribute(SESSION_KEY);
  89.        
  90.        // save IMG
  91.         Part filePart_img = request.getPart("file_img");
  92.         String fileName_img = Paths.get(filePart_img.getSubmittedFileName()).getFileName().toString();
  93.         InputStream fileContent_img = filePart_img.getInputStream();
  94.         byte[] bytes_Key_img = IOUtils.toByteArray(fileContent_img);
  95.            
  96.         String full_PATH_img = PATH_IMG_SIGN + fileName_img ;  
  97.            try {
  98.                writeBytesToFileNio(bytes_Key_img, full_PATH_img);
  99. //              String save_IMG_PATH = saveTo(fileContent_img, full_PATH_img);
  100. //              request.getSession().setAttribute(SESSION_IMG_KEY, save_IMG_PATH);
  101.                }catch (Exception e) {
  102.                     e.printStackTrace();
  103.                     LogoutServlet.doLogout(request, response, "System error !! Can not save IMG in server");
  104.                }
  105.         String save_IMG_PATH = (String) request.getSession().getAttribute(SESSION_IMG_KEY);
  106.        
  107.        
  108.         //send to check at
  109.         try {
  110.        
  111.         SignatureProfile sessionUploadKey = null;
  112.         sessionUploadKey.generateNewProfile( profileName, bytes_Key, password,  user_id);
  113.         System.out.println("Check Key success!!");
  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.             try {
  126.                 profileDAO.save(profileDB);
  127.                 System.out.println("Save Key success!!");
  128.             } catch (Exception e) {
  129.                 // TODO Auto-generated catch block
  130.                 e.printStackTrace();
  131.                 LogoutServlet.doLogout(request, response, "Error can not Save Key in DB!!" + e.getMessage());
  132.             }
  133.        
  134.        
  135.        
  136.        
  137.     }
  138.    
  139.     private static void writeBytesToFileNio(byte[] bFile, String fileDest) {
  140.  
  141.         try {
  142.             Path path = Paths.get(fileDest);
  143.             Files.write(path, bFile);
  144.         } catch (IOException e) {
  145.             e.printStackTrace();
  146.         }
  147.  
  148.     }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement