首页>代码>java servlet调用百度AI开放平台动物识别API和百度百科API实现动物识别的实例>/Crawler/src/main/java/com/zooseefun/servlet/CoreServlet.java
package com.zooseefun.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zooseefun.core.Core; import com.zooseefun.util.CrawlerUtil; import com.zooseefun.util.ImageUtil; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @WebServlet(name = "/CoreServlet", urlPatterns = { "/core" }, loadOnStartup = 1) public class CoreServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; /** * Constructor of the object. */ public CoreServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setHeader("Content-type", "text/html;charset=UTF-8"); response.setContentType("text/html;charset=utf-8"); Core core = new Core(); JSONObject json = new JSONObject(); JSONArray jsonArray = new JSONArray(); ImageUtil imageUtil = new ImageUtil(); String uploadPath = request.getParameter("uploadPath"); String path = imageUtil.receImage(request, uploadPath);//图片上传 @SuppressWarnings("static-access") String results = core.animal(JSONObject.fromObject(path).getString("imagePath"));//识别动物 JSONArray animalArray = JSONArray.fromObject(JSONObject.fromObject(results).get("result").toString()); for (int i = 0; i < animalArray.length(); i++) { JSONObject animal=new JSONObject(); JSONObject animals = JSONObject.fromObject(animalArray.get(i)); String animalname=animals.get("name").toString(); JSONObject animalInfo=JSONObject.fromObject(CrawlerUtil.getAnimalInfo(animalname)); String result = core.getResult(animalname); animal.put("name", animalname); animal.put("enname", result); animal.put("score", animals.get("score").toString()); animal.put("detail", animalInfo.get("info")); animal.put("img", animalInfo.get("image")); animal.put("desc", animalInfo.get("desc")); jsonArray.put(animal); } json.put("imagePath", JSONObject.fromObject(path).getString("url")); json.put("info", jsonArray); System.out.println(json.toString()); response.getWriter().print(json.toString()); } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ public void init() throws ServletException { // Put your code here } }
最近下载更多
扫把多啦攻core LV1
2022年6月5日
ruishent LV6
2022年3月8日
helenli522 LV1
2021年5月3日
13261713015 LV4
2021年1月4日
guodaxia LV14
2020年8月31日
磊哥哥哥哥 LV13
2020年5月28日
linghongjun5002 LV10
2020年4月24日
wei112233 LV15
2020年4月21日
zq980707 LV6
2020年4月3日
smartgood1 LV3
2019年4月11日
最近浏览更多
a1677596408 LV23
4月26日
诺一啊啊啊啊啊 LV3
4月7日
3334004690 LV3
3月20日
1134116035
2月25日
暂无贡献等级
tr1314qq LV17
2月5日
暂无贡献等级
2017143155 LV12
2023年6月27日
泓鼎168 LV19
2023年5月31日
lbsers LV5
2023年5月8日
xingbing LV9
2023年2月10日