天天看點

2020部落格之星投票總數排行 | 線上檢視

csdn2020部落格之星線上

2020部落格之星投票總數排行:http://blogstart2020.cyouagain.cn:81/index.html

2020部落格之星投票正在熱火朝天進行中,為友善大家檢視各位大佬的投票資料,我對csdn提供的資料進行整理。

主要代碼如下:

controller層:

package com.cya.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

@RestController
@CrossOrigin(origins = "*",maxAge = 3600)
public class BlogController {

    @RequestMapping("/getStartTop")
    public String getStartTop(){
        String url="https://bss.csdn.net/m/topic/blog_star2020/getUsers";
        String param="";
        String result = doPost(url, param);

        System.out.println("result="+result);
        return result;
    }


    public static String doPost(String httpUrl, String param) {

        HttpURLConnection connection = null;
        InputStream is = null;
        OutputStream os = null;
        BufferedReader br = null;
        String result = null;
        try {
            URL url = new URL(httpUrl);
            // 通過遠端url連接配接對象打開連接配接
            connection = (HttpURLConnection) url.openConnection();
            // 設定連接配接請求方式
            connection.setRequestMethod("POST");
            // 設定連接配接主機伺服器逾時時間:15000毫秒
            connection.setConnectTimeout(15000);
            // 設定讀取主機伺服器傳回資料逾時時間:60000毫秒
            connection.setReadTimeout(60000);

            // 預設值為:false,當向遠端伺服器傳送資料/寫資料時,需要設定為true
            connection.setDoOutput(true);
            // 預設值為:true,目前向遠端服務讀取資料時,設定為true,該參數可有可無
            connection.setDoInput(true);
            // 設定傳入參數的格式:請求參數應該是 name1=value1&name2=value2 的形式。
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 設定鑒權資訊:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0
            connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
            // 通過連接配接對象擷取一個輸出流
            os = connection.getOutputStream();
            // 通過輸出流對象将參數寫出去/傳輸出去,它是通過位元組數組寫出的
            os.write(param.getBytes());
            // 通過連接配接對象擷取一個輸入流,向遠端讀取
            if (connection.getResponseCode() == 200) {

                is = connection.getInputStream();
                // 對輸入流對象進行包裝:charset根據工作項目組的要求來設定
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

                StringBuffer sbf = new StringBuffer();
                String temp = null;
                // 循環周遊一行一行讀取資料
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 關閉資源
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 斷開與遠端位址url的連接配接
            connection.disconnect();
        }
        return result;
    }
}

           

js:

var URL="http://39.104.64.124:81/getStartTop"

$(document).ready(function(){

	getStart2020()
})

function getStart2020(){
	var data={};
	 $.ajax({
            //請求方式
            type : "POST",
            asnyc: false,
            //請求的媒體類型
         
            //請求位址
            url : URL,
            //資料,json字元串
            data : data,
            //請求成功
            dataType: 'json',
            success : function(result) {
                sort(result.data);
            },
            //請求失敗,包含具體的錯誤資訊
            error : function(e){
                console.log(e);
            }
        });}

        function sort(arr) {
            console.log("s:",arr.length)
          for(var i=0;i<arr.length-1;i++){
              var temp=arr[i];
              var index=i;
              for(var j=i+1;j<arr.length;j++){
                  if(arr[j].vote_num>arr[index].vote_num) index=j;
              }
              arr[i]=arr[index];
              arr[index]=temp;
          }

          console.log("arr:",arr);
          for(var i=0;i<arr.length;i++){
                  arr[i].title="https://blog.csdn.net/"+arr[i].title;

          }
          vue.dataArr=arr;
        }


var vue=new Vue({
    el:'#app',
    data:{
        dataArr:[]
    }
})
           

關注公衆号擷取更多IT技術文章