Advertisement
ballchaichana

uploadkey

Aug 9th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.58 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.Paths;
  9.  
  10. import javax.servlet.ServletException;
  11. import javax.servlet.annotation.MultipartConfig;
  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. import javax.servlet.http.Part;
  17.  
  18. import org.apache.commons.io.IOUtils;
  19.  
  20. import th.in.oneauthen.object.SignatureProfileDB;
  21. import th.in.oneauthen.object.UserUidDB;
  22. import th.in.oneauthen.object.DAO.SignatureProfileDAO;
  23.  
  24. import th.in.oneauthen.signing.SignatureProfile;;
  25. @WebServlet("/uploadKey")
  26. @MultipartConfig
  27. public class UploadKeyServlet extends HttpServlet {
  28.    
  29.     /**
  30.      *
  31.      */
  32.     private static final long serialVersionUID = 1L;
  33.  
  34.  
  35. //  public static final String SESSION_PARAM_USER = "userSession";
  36.  
  37.    
  38.     public static final String SYSTEM_SIGNATURE_PROFILE = "keyname";
  39.     public static final String KEY_PIN = "password";
  40.     public static final String SIGING_IMG = "img";
  41.    
  42.     public static String SESSION_IMG_KEY = "";
  43.     public static String SESSION_KEY = "";
  44.    
  45.     public static String PATH_KEY ="C:\\user\\Desktop\\key\\";
  46.     public static String PATH_IMG_SIGN ="C:\\user\\Desktop\\img_sign\\";
  47.    
  48.     public UploadKeyServlet() {
  49.         super();
  50.         // TODO Auto-generated constructor stub
  51.     }
  52.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  53.             throws ServletException, IOException {
  54.         // TODO Auto-generated method stub
  55.         doPost(request, response);
  56.     }
  57.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  58.             throws ServletException, IOException {
  59.        
  60.         //user_uid
  61.         UserUidDB userID = (UserUidDB) request.getSession().getAttribute(LoginServlet.SESSION_PARAM_USER);
  62.         int user_id =userID.getUserId();
  63.        
  64.         // profileName
  65.         String profileName = request.getParameter("description"); // Retrieves <input type="text" name="description">
  66.         // password or key_pin
  67.         String password = request.getParameter("key_pin");
  68.        
  69.        
  70.         // save KEY
  71.         Part filePart = request.getPart("file"); // 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.             saveTo(fileContent, 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.            
  93.         String full_PATH_img = PATH_IMG_SIGN + fileName_img ;  
  94.            try {
  95.                saveTo(fileContent_img, full_PATH_img);
  96.                 String save_IMG_PATH = saveTo(fileContent_img, full_PATH_img);
  97.                 request.getSession().setAttribute(SESSION_IMG_KEY, save_IMG_PATH);
  98.                }catch (Exception e) {
  99.                     e.printStackTrace();
  100.                     LogoutServlet.doLogout(request, response, "System error !! Can not save IMG in server");
  101.                }
  102.         String save_IMG_PATH = (String) request.getSession().getAttribute(SESSION_IMG_KEY);
  103.        
  104.        
  105.         //send to check at
  106.         try {
  107.        
  108.         SignatureProfile sessionUploadKey = null;
  109.         sessionUploadKey.generateNewProfile( profileName, bytes_Key, password,  user_id);
  110.         System.out.println("Check Key success!!");
  111.         } catch (Exception e) {
  112.             e.printStackTrace();
  113.             LogoutServlet.doLogout(request, response, "GenerateNewProfile Error !" + e.getMessage());
  114.         }
  115.        
  116.         SignatureProfileDAO profileDAO = new SignatureProfileDAO();
  117.         SignatureProfileDB profileDB = new SignatureProfileDB();
  118.             profileDB.setProfileName(profileName);
  119.             profileDB.setProfileKey(save_KEY_PATH);
  120.             profileDB.setSigImg(save_IMG_PATH);
  121.             profileDB.setProfileKeyPIN(password);
  122.             try {
  123.                 profileDAO.save(profileDB);
  124.                 System.out.println("Save Key success!!");
  125.             } catch (Exception e) {
  126.                 // TODO Auto-generated catch block
  127.                 e.printStackTrace();
  128.                 LogoutServlet.doLogout(request, response, "Error can not Save Key in DB!!" + e.getMessage());
  129.             }
  130.        
  131.        
  132.        
  133.        
  134.     }
  135.    
  136.     public String saveTo(InputStream file , String path) {
  137.  
  138.        
  139.         OutputStream outputStream = null;
  140.  
  141.         try {
  142.            
  143.  
  144.             // write the inputStream to a FileOutputStream
  145.             outputStream =
  146.                         new FileOutputStream(new File(path));
  147.  
  148.             int read = 0;
  149.             byte[] bytes = new byte[1024];
  150.  
  151.             while ((read = file.read(bytes)) != -1) {
  152.                 outputStream.write(bytes, 0, read);
  153.             }
  154.  
  155.             System.out.println("Done!");
  156.  
  157.         } catch (IOException e) {
  158.             e.printStackTrace();
  159.         } finally {
  160.             if (file != null) {
  161.                 try {
  162.                     file.close();
  163.                 } catch (IOException e) {
  164.                     e.printStackTrace();
  165.                 }
  166.             }
  167.             if (outputStream != null) {
  168.                 try {
  169.                     // outputStream.flush();
  170.                     outputStream.close();
  171.                 } catch (IOException e) {
  172.                     e.printStackTrace();
  173.                 }
  174.  
  175.             }
  176.         }
  177.         return path;
  178.         }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement