Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package th.in.oneauth.servlet;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.MultipartConfig;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.Part;
- import org.apache.commons.io.IOUtils;
- import th.in.oneauthen.object.SignatureProfileDB;
- import th.in.oneauthen.object.UserUidDB;
- import th.in.oneauthen.object.DAO.SignatureProfileDAO;
- import th.in.oneauthen.signing.SignatureProfile;;
- @WebServlet("/uploadKey")
- @MultipartConfig
- public class UploadKeyServlet extends HttpServlet {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- // public static final String SESSION_PARAM_USER = "userSession";
- public static final String SYSTEM_SIGNATURE_PROFILE = "keyname";
- public static final String KEY_PIN = "password";
- public static final String SIGING_IMG = "img";
- public static String SESSION_IMG_KEY = "";
- public static String SESSION_KEY = "";
- public static String PATH_KEY ="C:\\user\\Desktop\\key\\";
- public static String PATH_IMG_SIGN ="C:\\user\\Desktop\\img_sign\\";
- public UploadKeyServlet() {
- super();
- // TODO Auto-generated constructor stub
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- // TODO Auto-generated method stub
- doPost(request, response);
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- //user_uid
- UserUidDB userID = (UserUidDB) request.getSession().getAttribute(LoginServlet.SESSION_PARAM_USER);
- int user_id =userID.getUserId();
- // profileName
- String profileName = request.getParameter("description"); // Retrieves <input type="text" name="description">
- // password or key_pin
- String password = request.getParameter("key_pin");
- // save KEY
- Part filePart = request.getPart("file"); // Retrieves <input type="file" name="file">
- String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
- InputStream fileContent = filePart.getInputStream();
- byte[] bytes_Key = IOUtils.toByteArray(fileContent);
- // request.getSession().setAttribute("key_PDF", bytes_Key);
- String full_PATH = PATH_KEY + fileName;
- try {
- writeBytesToFileNio(bytes_Key, full_PATH);
- // String save_KEY_PATH = saveTo(fileContent, full_PATH);
- // request.getSession().setAttribute(SESSION_KEY, save_KEY_PATH);
- } catch (Exception e) {
- e.printStackTrace();
- LogoutServlet.doLogout(request, response, "System error !! Can not save KEY in srver");
- }
- String save_KEY_PATH = (String) request.getSession().getAttribute(SESSION_KEY);
- // save IMG
- Part filePart_img = request.getPart("file_img");
- String fileName_img = Paths.get(filePart_img.getSubmittedFileName()).getFileName().toString();
- InputStream fileContent_img = filePart_img.getInputStream();
- byte[] bytes_Key_img = IOUtils.toByteArray(fileContent_img);
- String full_PATH_img = PATH_IMG_SIGN + fileName_img ;
- try {
- writeBytesToFileNio(bytes_Key_img, full_PATH_img);
- // String save_IMG_PATH = saveTo(fileContent_img, full_PATH_img);
- // request.getSession().setAttribute(SESSION_IMG_KEY, save_IMG_PATH);
- }catch (Exception e) {
- e.printStackTrace();
- LogoutServlet.doLogout(request, response, "System error !! Can not save IMG in server");
- }
- String save_IMG_PATH = (String) request.getSession().getAttribute(SESSION_IMG_KEY);
- //send to check at
- try {
- SignatureProfile sessionUploadKey = null;
- sessionUploadKey.generateNewProfile( profileName, bytes_Key, password, user_id);
- System.out.println("Check Key success!!");
- } catch (Exception e) {
- e.printStackTrace();
- LogoutServlet.doLogout(request, response, "GenerateNewProfile Error !" + e.getMessage());
- }
- SignatureProfileDAO profileDAO = new SignatureProfileDAO();
- SignatureProfileDB profileDB = new SignatureProfileDB();
- profileDB.setProfileName(profileName);
- profileDB.setProfileKey(full_PATH);
- profileDB.setSigImg(full_PATH_img);
- profileDB.setProfileKeyPIN(password);
- try {
- profileDAO.save(profileDB);
- System.out.println("Save Key success!!");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- LogoutServlet.doLogout(request, response, "Error can not Save Key in DB!!" + e.getMessage());
- }
- }
- private static void writeBytesToFileNio(byte[] bFile, String fileDest) {
- try {
- Path path = Paths.get(fileDest);
- Files.write(path, bFile);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement