承接学习记录1
发布:
把your_localhost_port、your_workspaces、your_datastores的值替换为有效值就可以了
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
public class ReleaseUtil {
public static void main(String[] args) {
try {
String url = "http://your_localhost_port/geoserver/rest/workspaces/your_workspaces/datastores/your_datastores/featuretypes";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 设置请求方法为PUT
con.setRequestMethod("POST");
// 设置请求头部信息
con.setRequestProperty("Content-Type", "application/xml");
con.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("admin:geoserver".getBytes()));
// 设置请求体数据
String data = "<featureType>\n" +
" <name>zhjzzy_jc_sqzy</name>\n" +
" <nativeName>zhjzzy_jc_sqzy</nativeName>\n" +
"</featureType>";
con.setDoOutput(true);
con.getOutputStream().write(data.getBytes("UTF-8"));
// 发送请求并获取响应
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应结果
System.out.println("Response Code: " + responseCode);
System.out.println("Response Body: " + response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
效果:
修改图层的标题名称同样:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
public class StatusUtil
{
public static void main(String[] args) {
try {
String url = "http://your_localhost_port/geoserver/rest/workspaces/your_workspaces/datastores/your_datastores/featuretypes/zhjzzy_jc_sqzy";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 设置请求方法为PUT
con.setRequestMethod("PUT");
// 设置请求头部信息
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("admin:geoserver".getBytes()));
// 设置请求体数据
String data = "{\n" +
" \"featureType\": {" +
" \"title\": \"测试一下zhjzzy_jc_sqzy\"\n" +
"}\n}";
con.setDoOutput(true);
con.getOutputStream().write(data.getBytes("UTF-8"));
// 发送请求并获取响应
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应结果
System.out.println("Response Code: " + responseCode);
System.out.println("Response Body: " + response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
效果:
测试成功,那完全可以把相同的部分提出来,编为工具类:
发布图层的工具类:
已知标题的标签为"title",故在发布图层时如果需要直接命名图层标题,以xml形式发送"<title>你要取的标题</title>"即可。
以此类推,其他图层属性也可直接指定,如果没有指定就为默认。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
/**
* 发布图层
*/
public class Util {
/**
* 发布图层工具
* @param localhostPort 端口号
* @param workspaces 工作空间
* @param dataStores 存储空间
* @param name 图层特征类型名称
* @param nativeName 图层特征类型本地名称
*/
public static void geoReleaseTool(String localhostPort, String workspaces, String dataStores, String name, String nativeName)
{
try {
String url = "http://"+localhostPort+"/geoserver/rest/workspaces/"+workspaces+"/datastores/"+dataStores+"/featuretypes";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 设置请求方法为POST
con.setRequestMethod("POST");
// 设置请求头部信息
con.setRequestProperty("Content-Type", "application/xml");
con.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("admin:geoserver".getBytes()));
// 设置请求体数据
String data1 = "<featureType>\n" +
" <name>"+name+"</name>\n" +
" <nativeName>"+nativeName+"</nativeName>\n" +
"</featureType>";
con.setDoOutput(true);
con.getOutputStream().write(data1.getBytes("UTF-8"));
// 发送请求并获取响应
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应结果
System.out.println("Response Code: " + responseCode);
System.out.println("Response Body: " + response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
修改标题的:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
/**
* 修改图层标题
*/
public class NameUtil {
/**
* 修改图层标题工具
* @param localhostPort 端口号
* @param workspaces 工作空间
* @param dataStores 储存空间
* @param name 图层特征类型名称
* @param afterName 修改后的标题
*/
public static void geoModifyNameTool(String localhostPort, String workspaces, String dataStores, String name, String afterName)
{
try {
String url = "http://"+localhostPort+"/geoserver/rest/workspaces/"+workspaces+"/datastores/"+dataStores+"/featuretypes/"+name;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 设置请求方法为PUT
con.setRequestMethod("PUT");
// 设置请求头部信息
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("admin:geoserver".getBytes()));
// 设置请求体数据
String data = "{\n" +
" \"featureType\": {" +
" \"title\": \""+afterName+"\"\n" +
"}\n}";
con.setDoOutput(true);
con.getOutputStream().write(data.getBytes("UTF-8"));
// 发送请求并获取响应
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应结果
System.out.println("Response Code: " + responseCode);
System.out.println("Response Body: " + response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试结果:
补充:以上的工具只能适用于shp图层的发布,因为其他形式的图层请求的接口和请求体都不同,下面会举类Giff形式的图层发布和存储空间的创建。
简单封了一个常量池:
package com.albedo.java.modules.util.http;
public interface Constant {
String ADMIN = "admin";
String GEOSERVER = "geoserver";
String HTTP = "http://";
String SLASH_GEOSERVER = "/geoserver";
String SLASH_REST = "/rest";
String SLASH_WORKSPACE = "/workspaces";
String SLASH_DATASTORES = "/datastores";
String SLASH_LAYERS = "/layers";
String SLASH_COVERAGESTORES = "/coveragestores";
String COVERAGES = "/coverages";
//HTTP-POST请求
String HTTP_POST = "POST";
//HTTP-GET请求
String HTTP_GET = "GET";
//HTTP-PUT请求
String HTTP_PUT = "PUT";
//HTTP-DELETE请求
String HTTP_DELETE = "DELETE";
//请求头部字段-Content-Type
String CONTENT_TYPE = "Content-Type";
//请求响应中的实体主体为JSON
String APPLICATION_JSON = "application/json";
String APPLICATION_SLASH = "application/";
//请求响应中的实体主体为XML
String APPLICATION_XML = "application/XML";
//请求头部字段-Authorization
String AUTHORIZATION = "Authorization";
//响应字符类型-GBK
String GBK = "GBK";
//响应状态码
String RESPONSE_CODE = "Response Code: ";
//响应体
String RESPONSE_BODY = "Response Body: ";
//基本身份验证
String BASIC = "Basic ";
//斜杠
String SLASH = "/";
String CMD = "cmd";
String CMD_DOT_EXE = "cmd.exe";
String SLASH_FEATURETYPES = "/featuretypes";
String MINUS = "-";
String MINUS_I = "-I";
String MINUS_S = "-s";
String SHP2PGSQL = "shp2pgsql";
String F_T_T_S = "4326";
String EPSG_4490 = "EPSG:4490";
String DOT_JSON = ".json";
String DOT_HTML = ".html";
}
下面的代码涉及到常量的我就不改了,大家对比上面的代码意会即可
giff---储存空间
/**
* 创建Tif的储存仓库
* @param localhostPort 端口号
* @param workspaceName 工作空间
* @param dataStoreName 储存空间名
* @param path tiff文件路径
*/
public static void createTifDataStores(String localhostPort, String workspaceName, String path, String dataStoreName)
{
try {
String url = "http://"+localhostPort+"/geoserver/rest/workspaces/"+workspaceName+"/coveragestores/datastores";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 设置请求方法为POST
con.setRequestMethod(Constant.HTTP_POST);
// 设置请求头部信息
con.setRequestProperty(Constant.CONTENT_TYPE, Constant.APPLICATION_JSON);
con.setRequestProperty(Constant.AUTHORIZATION, Constant.BASIC + Base64.getEncoder().encodeToString("admin:geoserver".getBytes()));
// 设置请求体数据
String data1 = "{\n" +
" \"coverageStore\": {\n" +
" \"name\": \""+dataStoreName+"\",\n" +
" \"type\": \"GeoTIFF\",\n" +
" \"enabled\": true,\n" +
" \"url\": \"file:"+path+"\",\n" +
" \"workspace\": {\n" +
" \"name\": \""+workspaceName+"\"\n" +
" }\n" +
" }\n" +
"}";
con.setDoOutput(true);
con.getOutputStream().write(data1.getBytes(Constants.UTF_8));
// 发送请求并获取响应
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应结果
System.out.println(Constant.RESPONSE_CODE + responseCode);
System.out.println(Constant.RESPONSE_BODY + response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
giff--图层发布
/**
* POST 发布图层工具
* @param localhostPort 端口号
* @param workspaces 工作空间
* @param dataStores 存储空间
* @param name 图层特征类型名称
* @param title 图层标题
*/
public static void geoTiffReleaseTool(String localhostPort, String workspaces, String dataStores, String name, String title)
{
try {
String url = "http://"+localhostPort+"/geoserver/rest/workspaces/"+workspaces+"/coveragestores/"+dataStores+"/coverages";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 设置请求方法为POST
con.setRequestMethod(Constant.HTTP_POST);
// 设置请求头部信息
con.setRequestProperty(Constant.CONTENT_TYPE, Constant.APPLICATION_JSON);
con.setRequestProperty(Constant.AUTHORIZATION, Constant.BASIC + Base64.getEncoder().encodeToString("admin:geoserver".getBytes()));
// 设置请求体数据
String data1 = "{\n" +
" \"coverage\": {\n" +
" \"name\": \""+name+"\",\n" +
" \"title\": \""+title+"\",\n" +
" \"abstract\": \""+name+"\",\n" +
" \"enabled\": true,\n" +
"\"srs\": \""+Constant.EPSG_4490+"\""+
" }\n" +
"}";
con.setDoOutput(true);
con.getOutputStream().write(data1.getBytes(Constants.UTF_8));
// 发送请求并获取响应
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 打印响应结果
System.out.println(Constant.RESPONSE_CODE + responseCode);
System.out.println(Constant.RESPONSE_BODY + response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
同理,能用此种方式实现更多的接口功能,例如删除:
/**
* DELETE 删除已发布的图层
* @param localhostPort 端口号
* @param workspaces 工作空间
* @param dataStores 储存仓库
* @param layerName 图层特征名
*/
public static void geoDeleteTool(String localhostPort, String workspaces, String dataStores, String layerName)
{
try {
String url = "http://"+localhostPort+"/geoserver/rest/layers/"+layerName;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 设置请求方法为DELETE
con.setRequestMethod("DELETE");
// 设置请求头部信息
con.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("admin:geoserver".getBytes()));
// 发送请求并获取响应
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
deleteTool(localhostPort, workspaces, dataStores, layerName);
// 打印响应结果
System.out.println("Response Code: " + responseCode);
System.out.println("Response Body: " + response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除工具,只有执行这一步才能再次发布曾经发布过的图层名
* @param localhostPort 端口号
* @param workspaces 工作空间
* @param dataStores 储存仓库
* @param layerName 图层特征名
*/
public static void deleteTool(String localhostPort, String workspaces, String dataStores, String layerName)
{
try {
String url = "http://"+localhostPort+"/geoserver/rest/workspaces/"+workspaces+"/datastores/"+dataStores+"/featuretypes/"+layerName+".html";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 设置请求方法为DELETE
con.setRequestMethod("DELETE");
// 设置请求头部信息
con.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("admin:geoserver".getBytes()));
// 发送请求并获取响应
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
再修改一下就可以配合前端传输的数据使用了捏,更多的接口功能不再赘述。