1 index.jsp
2
3
4 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
5
6 <html>
7 <head>
8 <title>Face Distinguish</title>
9 <style type="text/css">
10 /* 第一种,标签名 */
11 body{background:#4DCDD2;font-family:"楷体";}
12 h1{
13 color:#ffffff;
14 text-shadow:-2px -5px 3px #000;
15 680px;
16 margin:0 auto;
17 text-align:center;
18 }
19 /* 第二种,以标签属性中class为box的标签加上此样式。 */
20 .box{
21 680px;
22 height:400px;
23 border:3px solid #000;
24 background:#fff;
25 margin:0 auto;
26 text-align:center;
27 box-shadow:9px 11px 5px #000;
28 }
29 .btn{
30 /* 添加区域块 */
31 display:block;
32 100px;
33 height:30px;
34 /* 区域块半径 */
35 border-radius:18px;
36 border:1px solid #000;
37 margin:10px auto;
38 /* 垂直高度 */
39 line-height:30px;
40 background:#00f;
41 color:#fff;
42 box-shadow:2px 2px 3px #000;
43 text-decoration:none;
44 }
45 .btn:hover{
46 margin-top:12px;
47 margin-left:auto;
48 background:#000;
49 }
50 </style>
51 <script type="text/javascript">
52 function openBrows(){
53 var object = document.getElementById("fileChoice");
54 object.click();
55 }
56 </script>
57 </head>
58
59 <body>
60 <h1>PaoZi--face distinguish</h1>
61 <form action="ImageUpload" method="post" enctype="multipart/form-data">
62 <!-- 表单提交区域 start -->
63 <div class="box">
64 <!-- 文件选择框 -->
65 <input type="file" id="fileChoice" name="imgfile" style="display:none;"/>
66 <a href="javascript:void(0);" class="btn" onclick="openBrows()">选择文件</a>
67 <input type="submit" value="提交解析" class="btn" />
68 </div>
69 <!-- end 表单提交区域 -->
70 </form>
71 </body>
72 </html>
1 web.xml
2
3
4 <?xml version="1.0" encoding="UTF-8"?>
5 <web-app version="3.0"
6 xmlns="http://java.sun.com/xml/ns/javaee"
7 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
8 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
9 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
10 <display-name></display-name>
11 <servlet>
12 <description>This is the description of my J2EE component</description>
13 <display-name>This is the display name of my J2EE component</display-name>
14 <servlet-name>ImageUpload</servlet-name>
15 <servlet-class>com.ImageUpload</servlet-class>
16 </servlet>
17
18 <servlet-mapping>
19 <servlet-name>ImageUpload</servlet-name>
20 <url-pattern>/ImageUpload</url-pattern>
21 </servlet-mapping>
22 <welcome-file-list>
23 <welcome-file>index.jsp</welcome-file>
24 </welcome-file-list>
25 </web-app>
1 ImageUpload.java
2
3
4 package com;
5
6 import java.io.*;
7 import java.util.List;
8 import java.util.UUID;
9
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14
15 import org.apache.commons.fileupload.FileItem;
16 import org.apache.commons.fileupload.FileItemFactory;
17 import org.apache.commons.fileupload.FileUploadException;
18 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
19 import org.apache.commons.fileupload.servlet.ServletFileUpload;
20
21
22 @SuppressWarnings("serial")//序列化(serialization)将对象的状态信息转换为可以存储或传输的形式的过程。
23 //@WebServlet(name="imgUp",urlPatterns={"/item.htm"})
24 public class ImageUpload extends HttpServlet {
25 @Override
26 public void service(HttpServletRequest request,HttpServletResponse response) throws IOException{
27 response.setContentType("text/html;charset=utf-8"); //设置编辑器所处理的内容类型
28 //只对post请求有效--start
29 request.setCharacterEncoding("utf-8");
30 //end--只对post请求有效
31 //get方式提交表单的接收--start
32 /*String name = request.getParameter("name");
33 //大部分浏览器,get方法以ISO-8859-1的编码方式提交
34 name = new String(name.getBytes("ISO-8859-1"),"utf-8");
35 response.getWriter().print(name + ": Hello ! 大家好,我就是袍子。");*/
36 //end--get方式提交表单的接收
37 //1.磁盘工厂,生产磁盘文件
38 FileItemFactory fif = new DiskFileItemFactory();
39 //2.上传工具
40 ServletFileUpload upload = new ServletFileUpload(fif);
41 //格式化request,获取数据
42 try {
43 List<FileItem> items = upload.parseRequest(request);
44 FileItem item = items.get(0);
45 //保存至服务器
46 //相对目录
47 String path = request.getSession().getServletContext().getRealPath("");
48 //随机生成文件的唯一标识uuid
49 String uuid = UUID.randomUUID().toString() + ".jpg";
50 //输出到磁盘的输出流
51 OutputStream out = new FileOutputStream(path + "/image/" + uuid);
52 request.setAttribute("imgPath", "image/" + uuid);
53 //获取文件上传的输入流
54 InputStream is = item.getInputStream();
55 byte[] buf = new byte[1024];
56 int len = 0;
57 while((len = is.read(buf)) != -1){
58 out.write(buf, 0, len);
59 }
60 is.close();
61 out.close();
62 } catch (FileUploadException e) {
63 e.printStackTrace();
64 }
65 try {
66 request.getRequestDispatcher("ShowImg.jsp").forward(request,response);
67 } catch (ServletException e) {
68 e.printStackTrace();
69 }
70 }
71 }
1 ShowImg.jsp
2
3
4 <%@ page import="org.json.JSONObject" %>
5 <%@ page import="org.json.JSONArray" %>
6 <%@ page import="com.facepp.http.PostParameters" %>
7 <%@ page import="com.facepp.http.HttpRequests" %>
8 <%@ page import="java.io.File" %>
9 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
10 <%
11 //创建一个请求,请求API接口
12 HttpRequests hsr = new HttpRequests("e7dd46080555b3c4f8b58c1341c09c5d","hL6swkaL4s2Zlzy3SKyvwRCCPK0EGPES");
13 //进行参数打包
14 PostParameters pps = new PostParameters();
15 String imgPath = request.getAttribute("imgPath").toString();
16 pps.setImg(new File(application.getRealPath("/") + "/" + imgPath));
17 JSONObject result = hsr.detectionDetect(pps);
18 JSONArray faces = result.getJSONArray("face");
19 StringBuffer strBuf = new StringBuffer();
20 for(int i = 0;i<faces.length();i++){
21 JSONObject face = faces.getJSONObject(i); //拿到i下标代表的脸部信息
22 JSONObject attribute = face.getJSONObject("attribute");
23 JSONObject age = attribute.getJSONObject("age");
24 int agevalue = age.getInt("value");
25 int agerange = age.getInt("range");
26 strBuf.append("Age : ").append(agevalue).append(" Range : ").append(agerange).append("<BR>");
27 /* JSONObject gender = face.getJSONObject("gender");
28 String gendervalue = gender.getString("value");
29 double genderconfidence = gender.getDouble("confidence");
30 strBuf.append("Gender : ").append(gendervalue).append(" confidence : ").append(genderconfidence).append("<BR>");
31 */
32 /* JSONObject race = face.getJSONObject("race");
33 String racevalue = race.getString("value");
34 double raceconfidence = race.getDouble("confidence");
35 strBuf.append("Race : ").append(racevalue).append(" confidence : ").append(raceconfidence).append("<BR>");
36 */
37 /* JSONObject smiling = face.getJSONObject("smiling");
38 double smilingvalue = smiling.getDouble("value");
39 strBuf.append("Smiling : ").append(smilingvalue).append("<BR>");
40 */ }
41 %>
42 <html>
43 <head>
44 <style type="text/css">
45 body{background:#4DCDD2;font-family:"楷体";}
46 h1{
47 color:#ffffff;
48 text-shadow:-2px -5px 3px #000;
49 680px;
50 margin:0 auto;
51 text-align:center;
52 }
53 .box{
54 680px;
55 height:400px;
56 border:3px solid #000;
57 background:#fff;
58 margin:0 auto;
59 text-align:left;
60 box-shadow:9px 11px 5px #000;
61 }
62 </style>
63 </head>
64
65 <body>
66 <h1>PaoZi--face distinguish</h1>
67 <div class="box">
68 <img style="float:left" src="${imgPath}" width="300" height="400">
69 <div style="float:center"><%=strBuf.toString() %></div>
70 </div>
71 </body>
72 </html>
http://www.faceplusplus.com.cn/