提取网络请求部分
This commit is contained in:
@@ -6,6 +6,7 @@ import cn.celess.blog.entity.model.QiniuResponse;
|
|||||||
import cn.celess.blog.exception.MyException;
|
import cn.celess.blog.exception.MyException;
|
||||||
import cn.celess.blog.service.CountService;
|
import cn.celess.blog.service.CountService;
|
||||||
import cn.celess.blog.service.QiniuService;
|
import cn.celess.blog.service.QiniuService;
|
||||||
|
import cn.celess.blog.util.HttpUtil;
|
||||||
import cn.celess.blog.util.RedisUtil;
|
import cn.celess.blog.util.RedisUtil;
|
||||||
import cn.celess.blog.util.ResponseUtil;
|
import cn.celess.blog.util.ResponseUtil;
|
||||||
import cn.celess.blog.util.VeriCodeUtil;
|
import cn.celess.blog.util.VeriCodeUtil;
|
||||||
@@ -176,40 +177,13 @@ public class Other {
|
|||||||
|
|
||||||
@GetMapping("/bingPic")
|
@GetMapping("/bingPic")
|
||||||
public Response bingPic() {
|
public Response bingPic() {
|
||||||
StringBuffer sb = new StringBuffer();
|
|
||||||
|
JSONObject imageObj;
|
||||||
try {
|
try {
|
||||||
//建立URL
|
imageObj = JSONObject.fromObject(HttpUtil.get("https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN"));
|
||||||
URL url = new URL("https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN");
|
} catch (IOException e) {
|
||||||
|
|
||||||
//打开http
|
|
||||||
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
|
|
||||||
urlConnection.setDoInput(true);
|
|
||||||
urlConnection.setRequestMethod("GET");
|
|
||||||
urlConnection.connect();
|
|
||||||
|
|
||||||
//获得输入
|
|
||||||
InputStream inputStream = urlConnection.getInputStream();
|
|
||||||
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
|
|
||||||
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
|
|
||||||
|
|
||||||
//将bufferReader的值给放到buffer里
|
|
||||||
String str = null;
|
|
||||||
while ((str = bufferedReader.readLine()) != null) {
|
|
||||||
sb.append(str);
|
|
||||||
}
|
|
||||||
//关闭bufferReader和输入流
|
|
||||||
bufferedReader.close();
|
|
||||||
inputStreamReader.close();
|
|
||||||
inputStream.close();
|
|
||||||
//断开连接
|
|
||||||
urlConnection.disconnect();
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return ResponseUtil.failure(null);
|
return ResponseUtil.failure(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONObject imageObj = JSONObject.fromObject(sb.toString());
|
|
||||||
JSONArray jsonArray = imageObj.getJSONArray("images");
|
JSONArray jsonArray = imageObj.getJSONArray("images");
|
||||||
String imageName = jsonArray.getJSONObject(0).getString("url");
|
String imageName = jsonArray.getJSONObject(0).getString("url");
|
||||||
return ResponseUtil.success("https://cn.bing.com" + imageName);
|
return ResponseUtil.success("https://cn.bing.com" + imageName);
|
||||||
|
|||||||
51
src/main/java/cn/celess/blog/util/HttpUtil.java
Normal file
51
src/main/java/cn/celess/blog/util/HttpUtil.java
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package cn.celess.blog.util;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: 小海
|
||||||
|
* @Date: 2020-04-23 15:51
|
||||||
|
* @Desc:
|
||||||
|
*/
|
||||||
|
public class HttpUtil {
|
||||||
|
|
||||||
|
public static String get(String urlStr) throws IOException {
|
||||||
|
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
|
||||||
|
HttpURLConnection urlConnection = null;
|
||||||
|
try {
|
||||||
|
URL url = new URL(urlStr);
|
||||||
|
|
||||||
|
//打开http
|
||||||
|
urlConnection = (HttpURLConnection) url.openConnection();
|
||||||
|
urlConnection.setDoInput(true);
|
||||||
|
urlConnection.setRequestMethod("GET");
|
||||||
|
urlConnection.connect();
|
||||||
|
|
||||||
|
try (
|
||||||
|
InputStream inputStream = urlConnection.getInputStream();
|
||||||
|
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
|
||||||
|
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
|
||||||
|
) {
|
||||||
|
//将bufferReader的值给放到buffer里
|
||||||
|
String str = null;
|
||||||
|
while ((str = bufferedReader.readLine()) != null) {
|
||||||
|
sb.append(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
//断开连接
|
||||||
|
if (urlConnection != null) {
|
||||||
|
urlConnection.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user