LoadFPTemplate.jsp 5.72 KB
Newer Older
Thitichaipun Wutthisak committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
<%@page contentType="text/html;  charset=UTF-8;" pageEncoding="UTF-8"  %>

<%@page import="java.io.ByteArrayInputStream" %>
<%@page import="java.io.ByteArrayOutputStream" %>
<%@page import="java.io.*" %>
<%@page import="java.io.IOException" %>
<%@page import="java.io.InputStream" %>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@page import="org.apache.commons.fileupload.FileItemFactory" %>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@page import="org.apache.commons.fileupload.FileItem" %>
<%@page import="com.csc.library.session.*" %>
<%@page import="com.csc.library.utilities.*,com.csc.library.database.DataSet,java.util.*" %>
<%@page import="com.csc.library.database.CscConnection" %>
<%@page import="com.csc.library.zkprocess.*" %>
<%@page import="com.csc.library.fputility.*" %>
<%@page import="com.google.gson.Gson" %>
<%@page import="com.google.gson.GsonBuilder" %>
<%@page import="com.csc.library.util.download.*"%>

<%

	try {
		System.out.print("!! Load emp picture has been called !!");
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);

		if(isMultipart){
			System.out.print("!! Multipart data detected [sv_getmenu]!!");
			// Create a factory for disk-based file items
			FileItemFactory factory = new DiskFileItemFactory();

			// Create a new file upload handler
			ServletFileUpload upload = new ServletFileUpload(factory);

			// Parse the request
			List /* FileItem */ items = upload.parseRequest(request);

			// Process the uploaded items
			Iterator iter = items.iterator();
			HashMap dataitem = new HashMap();
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();

				if (item.isFormField()) {
					String name = item.getFieldName();
					String value = item.getString();
					dataitem.put(name, value);
					System.out.print("!! Field name : "+ name + " value " + value);
				} else {
					System.out.print("!! File upload detected !!");
					dataitem.put("attchfile", item);
				}
			}

			/* save detail */
			
			long curtime = System.currentTimeMillis();

			if(dataitem.containsKey("data")){
				try {
					//System.out.print("input upd emp info --> " + (String) dataitem.get("data"));
					/* testing process will be sent in normal http get */
					String str = (String) dataitem.get("data") ;
					ZKPackageBase zkpack = new ZKPackageBase(str);
										
					SV_GetEmpPicture getdev = new SV_GetEmpPicture(zkpack);
					getdev.run();
					ThaiUtilities thai = new ThaiUtilities();
					if(zkpack.isAuth_status()){
						String filePath =  new InitialEnvironment("GLOBAL").getValue("ZKFP_UPLOAD-dir") +"/PICTURES/100/"+zkpack.getFPTemplate();
						System.out.print("!! emp file : " + filePath);
						File f = new File(thai.ASCII2Unicode(filePath));
						String fileName = f.getName();
						String fileType = fileName.substring(fileName.indexOf(".")+1,fileName.length());
						if ( f.exists() && f.canRead() ) {			
							response.setContentType("application/octet-stream");
							response.setHeader("Content-Disposition", "attachment; filename="+thai.Unicode2ASCII(f.getName()));
							//response.setHeader("cache-control", "no-cache");
							response.setContentLength((int) f.length());
							BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(f));
							byte buffer[] = new byte[8 * 1024];
							out.clearBuffer();
							OutputStream out_s = response.getOutputStream();
							CopyStreams.copyStreamsWithoutClose(fileInput, out_s, buffer);
							out_s.flush();
							out_s.close();
							fileInput.close();
						}
					}

				}catch(Exception e){
					System.out.print(e.getMessage());
				}
			}

			if(dataitem.containsKey("attchfile")){
				// leave blank
			}
		}else{
			/* testing process will be sent in normal http get */
			/*
			String str = "y4jsIdLJ5lFcHX05+91LzwNn62ePhKPco0ibyweOmZ2t63Gai3m3pr+0Wx8VlZ7ky7RF66xDjYYKaGQ0j6s3DA==";
			
			MyFPSecurity mysec = new MyFPSecurity();
			out.print(mysec.decrypt(str, "UTF-8"));*/

			/*
			String str = "username:myhradmin,password:Zmks094xWGx,picture:4F36B16NJU4.jpg";
			MyFPSecurity mysec = new MyFPSecurity();
			out.print(mysec.encrypt(str, "UTF-8"));*/

			
			//out.print("<br>!! TEST LOAD EMPLOYEE PIC INFO!!<br><br>");
			//String str = "y4jsIdLJ5lFcHX05+91LzwNn62ePhKPco0ibyweOmZ2t63Gai3m3pr+0Wx8VlZ7ky7RF66xDjYYKaGQ0j6s3DA==";
			String str = "username:myhradmin,password:Zmks094xWGx,template:1435312899382.dat";
			ZKPackageBase zkpack = new ZKPackageBase(str);
					
			SV_GetEmpPicture getdev = new SV_GetEmpPicture(zkpack);
			getdev.run();
			
			ThaiUtilities thai = new ThaiUtilities();

			//out.print("-->" + zkpack.isAuth_status());
			if(zkpack.isAuth_status()){
				String filePath =  new InitialEnvironment("GLOBAL").getValue("ZKFP_UPLOAD-dir") +"/PICTURES/100/"+zkpack.getFPTemplate();
				System.out.print("!! emp file : " + filePath);
				File f = new File(thai.ASCII2Unicode(filePath));
				String fileName = f.getName();
				String fileType = fileName.substring(fileName.indexOf(".")+1,fileName.length());
				if ( f.exists() && f.canRead() ) {			
					response.setContentType("application/octet-stream");
					response.setHeader("Content-Disposition", "attachment; filename="+thai.Unicode2ASCII(f.getName()));
					//response.setHeader("cache-control", "no-cache");
					response.setContentLength((int) f.length());
					BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(f));
					byte buffer[] = new byte[8 * 1024];
					out.clearBuffer();
					OutputStream out_s = response.getOutputStream();
					CopyStreams.copyStreamsWithoutClose(fileInput, out_s, buffer);
					out_s.flush();
					out_s.close();
					fileInput.close();
				}
			}
		}
	}catch(Exception e){
		System.out.print("xxx"+e.getMessage());
	}

	//out.print(recdata.getPackage());
	
%>