zoukankan      html  css  js  c++  java
  • 通过公开的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();
        }
  • 相关阅读:
    Android网络框架之Retrofit + RxJava + OkHttp 变化的时代
    网易与Google合作发布开源UI自动化测试方案 牛逼:Google 方面评价,这可能是目前世界上最好的 Android 游戏自动化测试方案。
    GitHub上受欢迎的Android UI Library
    推荐一些socket工具,TCP、UDP调试、抓包工具 (转载)
    pytest 一个测试类怎样使用多种fixture前置方法
    Appium服务器初始化参数(Capability)
    pytest执行用例:明明只写了5个测试用例, 怎么收集到33个!?
    解决VirtualBox 运行时报内存不能written
    VirtualBox 虚拟机怎样设置共享文件夹
    简单通俗讲解 android 内存泄漏
  • 原文地址:https://www.cnblogs.com/oldcownotGiveup/p/5404158.html
Copyright © 2011-2022 走看看