• 通过公开的API获取图片并且下载保存


    首页显示

     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();
    10                     $("input[value=save]").remove();
    11                     var html="";
    12                     for(var i=0;i<returnedData.length;i++){
    13                         html+="<img src='"+returnedData[i]+"'width='300' height='300'>";
    14                     }
    15                     
    16                     $("#div1").append(html);
    17                     $("input[value=clickme]").after("<input type='button' id='save1' value='save'>");
    18                     $("#save1").click(function(){
    19                         $("#div2").html("procession");
    20                         $.post("SaveImageServlet",
    21                                 {},
    22                         function(){
    23                             
    24                         });
    25                     });
    26                     //alert($("select :selected").val());
    27                 });
    28         });
    29 });
    30     </script>
    31   </head>
    32   
    33   <body>
    34   <select id="select1">
    35   <option value="1">picture1</option>
    36   <option value="2">picture2</option>
    37   <option value="3">picture3</option>
    38   <option value="4" selected>picture4</option>
    39   <option value="5">picture5</option>
    40   <option value="">picture6</option>
    41   <option value="7">picture7</option>
    42   <option value="8">updatepicture</option>
    43   
    44   
    45   </select>
    46   <!-- <input type="text" id="num"> -->
    47    <input type="button" id="button1" value="clickme">
    48    <div id="div2"></div>
    49    <div id="div1"></div>
    50   </body>
    51 </html>

    ajax提交数据到GsonServlet

    package com.zhanghaobo.servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import org.json.JSONArray;
    import org.json.JSONObject;
    
    import com.google.gson.Gson;
    import com.zhanghaobo.model.*;
    
    public class GsonServlet extends HttpServlet {
    
    
        /**
         * 通过id值向目标网站获取图片信息
         */
        private static final long serialVersionUID = 1L;
    
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
    
        }
    
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
                //用来存放获取的图片信息
                ArrayList<String> returnlist=new ArrayList<String>();
                HttpSession session=request.getSession();
                //在session中存放
                session.setAttribute("images",returnlist);
                String queryString=null;
                String queString=null;
                try {
                    int num=Integer.parseInt(request.getParameter("id"));
                    session.setAttribute("number", num);
                    //System.out.println(NetWorkUtil.getStringContentFromURL(queryString));
                    if(num!=8){
                    queryString=new StringBuffer(Constant.url1).append(num).toString();
                    }else{
                    queryString=new StringBuffer(Constant.urlupdate).toString();
                    }
                    //从网站获取JSON数据
                    JSONObject json11=new JSONObject(NetWorkUtil.getStringContentFromURL(queryString));
                    //获取行数
                    int sum=json11.getInt("total");
                    if(num!=8){
                    queString=new StringBuffer(Constant.url1).append(num).append("&rows=").append(sum).toString();
                    }else{
                    queString=new StringBuffer(Constant.urlupdate).append("?rows=").append(sum).toString();
                    }
                    JSONObject json12=new JSONObject(NetWorkUtil.getStringContentFromURL(queString));
                    JSONArray imgs=json12.getJSONArray("tngou");
                    System.out.println(num);
                    for(int i=0;i<imgs.length();i++){
                        JSONObject img=imgs.getJSONObject(i);
                        String result=img.getString("img");
                        //System.out.println(result);
                        StringBuffer sb=new StringBuffer();
                        sb.append(Constant.url).append(result);
                        returnlist.add(sb.toString());
                    }
                    //把信息用Gson进行数据传递
                    Gson gjson=new Gson();
                    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();
                }
            
        }
    
    
    }

    辅助类

    public class NetWorkUtil {
        public static String getStringContentFromURL(String queryString) throws Exception{
            String str=queryString;
            //获取连接
            URLConnection conn=new URL(str).openConnection();
            //读取输入流
            InputStream is=conn.getInputStream();
            //字节流字符流转换
            InputStreamReader isr=new InputStreamReader(is);
            BufferedReader br=new BufferedReader(isr);
            
            StringBuffer buffer=new StringBuffer();
            String line=null;
            while(null!=(line=br.readLine())){
                buffer.append(line);
            }
            br.close();
            isr.close();
            is.close();
            //System.out.println(buffer);
            return buffer.toString();
        }
  • 相关阅读:
    "Coding Interview Guide" -- 删除无序单链表中值重复出现的节点
    "Coding Interview Guide" -- 设计一个有getMin功能的栈
    "Coding Interview Guide" -- 仅用递归函数和栈操作逆序一个栈
    "Coding Interview Guide" -- 猫狗队列
    "Coding Interview Guide" -- 阶乘
    "Coding Interview Guide" -- 不用额外变量交换两个整数的值
    IDEA问题
    JAVA
    Log4j
    MAVEN教程
  • 原文地址:https://www.cnblogs.com/oldcownotGiveup/p/5404158.html
Copyright © 2020-2023  润新知