package org.springblade.system.expand.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

/**
 * @author lidexi
 * @date 2021/5/27 13:47
 */
@RestController
@RequestMapping("/sysarea")
public class SysAreaController {

	@GetMapping("/getFullJson")
	@ApiOperationSupport(order = 13)
	@ApiOperation(value = "获取地址json", notes = "")
	@ApiIgnore
	public R getFullJson(String url) {

		String result = "";
		BufferedReader in = null;
		try {
			String urlNameString = url;
			URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
			URLConnection connection = realUrl.openConnection();
// 建立实际的连接
			connection.connect();
// 获取所有响应头字段
			Map map = connection.getHeaderFields();
// 定义 BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(
				connection.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			System.out.println("发送GET请求出现异常！" + e);
			e.printStackTrace();
		}
// 使用finally块来关闭输入流
		finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
//得到的json数据
		System.out.println(result);
		return R.data(result);

	}
}
