Get pictures through the public API and download save

Source: Internet
Author: User

Home Display

1<script type= "Text/javascript" src= "Scripts/jquery-2.2.1.js" ></script>2<script type= "Text/javascript" >3 $ (function () {4$ ("#button1"). Click (function () {5$.post ("Gsonservlet",6{"id": $ ("select:selected")). Val ()},7 function (returneddata,status) {8                     //alert (returneddata.length);9$ ("#div1"). empty ();Ten$ ("Input[value=save")). Remove (); Onevar html= ""; A                      for(Var i=0;i<returneddata.length;i++){ -html+= " '; -                     } the                      -$ ("#div1"). append (HTML); -$ ("Input[value=clickme]"). After ("<input type= ' button ' id= ' save1 ' value= ' save ' >"); -$ ("#save1"). Click (function () { +$ ("#div2"). HTML ("procession")); -$.post ("Saveimageservlet", +                                 {}, A function () { at                              -                         }); -                     }); -                     //Alert ($ ("select:selected"). Val ()); -                 }); -         }); in }); -</script> to +    -<body> the<select id= "Select1" > *<option value= "1" >picture1</option> $<option value= "2" >picture2</option>Panax Notoginseng<option value= "3" >picture3</option> -<option value= "4" selected>picture4</option> the<option value= "5" >picture5</option> +<option value= "" >picture6</option> A<option value= "7" >picture7</option> the<option value= "8" >updatepicture</option> +    -    $</select> $<!--<input type= "text" id= "num" >- -<input type= "button" id= "Button1" value= "ClickMe" > -<div id= "Div2" ></div> the<div id= "Div1" ></div> -</body>Wuyi

Ajax submits data to Gsonservlet

 PackageCom.zhanghaobo.servlet;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjava.util.ArrayList;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpSession;ImportOrg.json.JSONArray;ImportOrg.json.JSONObject;ImportCom.google.gson.Gson;ImportCom.zhanghaobo.model.*; Public classGsonservletextendsHttpServlet {/*** Get picture information from the target website by ID value*/    Private Static Final LongSerialversionuid = 1L;  Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {} Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//used to store the acquired picture informationArraylist<string> returnlist=NewArraylist<string>(); HttpSession Session=request.getsession (); //Store in SessionSession.setattribute ("Images", returnlist); String queryString=NULL; String questring=NULL; Try {                intNum=integer.parseint (Request.getparameter ("id")); Session.setattribute ("Number", num); //System.out.println (Networkutil.getstringcontentfromurl (queryString));                if(num!=8) {queryString=NewStringBuffer (CONSTANT.URL1). Append (num). toString (); }Else{queryString=NewStringBuffer (constant.urlupdate). toString (); }                //get JSON data from a Web siteJsonobject json11=NewJsonobject (Networkutil.getstringcontentfromurl (queryString)); //Get row Count                intSum=json11.getint ("Total"); if(num!=8) {questring=NewStringBuffer (CONSTANT.URL1). Append (num). Append ("&rows="). Append (sum). toString (); }Else{questring=NewStringBuffer (constant.urlupdate). Append ("? rows="). Append (sum). toString (); } jsonobject json12=NewJsonobject (Networkutil.getstringcontentfromurl (questring)); Jsonarray IMGs=json12.getjsonarray ("Tngou");                SYSTEM.OUT.PRINTLN (num);  for(intI=0;i) {jsonobject img=Imgs.getjsonobject (i); String result=img.getstring ("img"); //System.out.println (result);StringBuffer sb=NewStringBuffer ();                    Sb.append (Constant.url). append (result);                Returnlist.add (Sb.tostring ()); }                //data transfer with Gson informationGson gjson=NewGson (); String Jsonres=Gjson.tojson (returnlist);                System.out.println (Jsonres); Response.setcontenttype ("Application/json;charset=utf-8"); Response.setheader ("Pragma", "No-cache"); Response.setheader ("Cache-control", "No-cache"); PrintWriter out=Response.getwriter ();                Out.println (Jsonres);                Out.flush ();            Out.close (); } Catch(Exception e) {e.printstacktrace (); }            }}

Auxiliary classes

 Public classNetworkutil { Public StaticString Getstringcontentfromurl (String queryString)throwsexception{String str=queryString; //Get ConnectionsURLConnection conn=NewURL (str). OpenConnection (); //Read input streamInputStream is=Conn.getinputstream (); //Stream conversion of Byte stream charactersInputStreamReader isr=NewInputStreamReader (IS); BufferedReader BR=NewBufferedReader (ISR); StringBuffer Buffer=NewStringBuffer (); String Line=NULL;  while(NULL! = (line=Br.readline ()))        {Buffer.append (line);        } br.close ();        Isr.close ();        Is.close (); //System.out.println (buffer);        returnbuffer.tostring (); }

Get pictures from the exposed API and download save

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.