对等方重置连接:套接字写入错误 httpclient 和 geoserver
Connection reset by peer: socket write error httpclient and geoserver
我需要将压缩的 shapefile 上传到 GeoServer through REST. The GeoServer provides an example CURL command 以上传 shapefile zip,如下所示:
curl -v -u admin:geoserver -XPUT -H "Content-type: application/zip" --data-binary @data.zip http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/Murrindindi/file.shp
上面的 CURL 命令工作正常。但是,我想在 Java httpclient 中上传。我是 Java 的新手,但我编写了以下代码来实现此目的:
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class QuickStart {
public static void main(String[] args) throws Exception {
String file="data.zip";
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials("admin", "geoserver"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpPut httpPut = new HttpPut("http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/Murrindindi/file.shp");
File file2 = new File("data.zip");
InputStream inputStream = new FileInputStream(file);
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("file", new FileBody(file2, ContentType.DEFAULT_BINARY))
.addBinaryBody("upstream", inputStream, ContentType.create("application/zip"), "data.zip")
.build();
httpPut.setEntity(reqEntity);
System.out.println("executing request " + httpPost.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpPut );
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
}
EntityUtils.consume(resEntity);
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
}
当我 运行 此代码时,出现以下错误:
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->http://172.16.17.86:9090:
Connection reset by peer: socket write error
Exception in thread "main" org.apache.http.client.ClientProtocolException
知道如何解决这个问题吗?我已经通过简单的身份验证测试了 POST 并且可以正常工作。但是,当我尝试上传 zip 文件时出现问题。
curl输出如下:
curl -v -u admin:geoserver -XPUT -H "Content-type: application/zip" --data-binary @Murrindindi_OvalCorrected.zip http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/Murrindindi/file.shp
* Trying 172.16.17.86...
* TCP_NODELAY set
* Connected to 172.16.17.86 (172.16.17.86) port 9090 (#0)
* Server auth using Basic with user 'shapeuploader'
> PUT /geoserver/rest/workspaces/IDIRA6/datastores/Murrindindi/file.shp HTTP/1.1
> Host: 172.16.17.86:9090
> Authorization: Basic c2hhcGV1cGxvYWRlcjo2OHJpOURwazdJR001bEo=
> User-Agent: curl/7.63.0
> Accept: */*
> Content-type: application/zip
> Content-Length: 7022252
> Expect: 100-continue
>
< HTTP/1.1 100
* We are completely uploaded and fine
< HTTP/1.1 201
< X-Frame-Options: SAMEORIGIN
< Content-Length: 0
< Date: Sat, 23 Mar 2019 23:04:27 GMT
* Connection #0 to host 172.16.17.86 left intact
将 curl 生成的原始 HTTP 数据与您的代码生成的数据进行比较后,我发现了以下差异:
- curl 不生成多部分请求
- curl 生成一个
Expect: 100-continue
header
- curl 使用抢占式基本身份验证
以下代码应该等同于该 curl 命令,但我无法使用 GeoServer 对其进行测试。我用过fluent API of apache-httpclient (that is fluent-hc-4.5.7.jar
)。让我知道它是否有效。
import java.io.File;
import org.apache.http.client.fluent.*;
import org.apache.http.entity.ContentType;
public class QuickStart {
public static void main(String[] args) throws Exception {
File file = new File("data.zip");
Executor executor = Executor.newInstance()
.auth("admin", "geoserver")
.authPreemptive("172.16.17.86:9090");
String response = executor.execute(Request.Put("http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/Murrindindi/file.shp")
.useExpectContinue()
.bodyFile(file, ContentType.create("application/zip")))
.returnResponse()
.toString();
System.out.println(response);
}
}
我需要将压缩的 shapefile 上传到 GeoServer through REST. The GeoServer provides an example CURL command 以上传 shapefile zip,如下所示:
curl -v -u admin:geoserver -XPUT -H "Content-type: application/zip" --data-binary @data.zip http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/Murrindindi/file.shp
上面的 CURL 命令工作正常。但是,我想在 Java httpclient 中上传。我是 Java 的新手,但我编写了以下代码来实现此目的:
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class QuickStart {
public static void main(String[] args) throws Exception {
String file="data.zip";
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials("admin", "geoserver"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpPut httpPut = new HttpPut("http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/Murrindindi/file.shp");
File file2 = new File("data.zip");
InputStream inputStream = new FileInputStream(file);
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("file", new FileBody(file2, ContentType.DEFAULT_BINARY))
.addBinaryBody("upstream", inputStream, ContentType.create("application/zip"), "data.zip")
.build();
httpPut.setEntity(reqEntity);
System.out.println("executing request " + httpPost.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpPut );
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
}
EntityUtils.consume(resEntity);
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
}
当我 运行 此代码时,出现以下错误:
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->http://172.16.17.86:9090:
Connection reset by peer: socket write error
Exception in thread "main" org.apache.http.client.ClientProtocolException
知道如何解决这个问题吗?我已经通过简单的身份验证测试了 POST 并且可以正常工作。但是,当我尝试上传 zip 文件时出现问题。
curl输出如下:
curl -v -u admin:geoserver -XPUT -H "Content-type: application/zip" --data-binary @Murrindindi_OvalCorrected.zip http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/Murrindindi/file.shp
* Trying 172.16.17.86...
* TCP_NODELAY set
* Connected to 172.16.17.86 (172.16.17.86) port 9090 (#0)
* Server auth using Basic with user 'shapeuploader'
> PUT /geoserver/rest/workspaces/IDIRA6/datastores/Murrindindi/file.shp HTTP/1.1
> Host: 172.16.17.86:9090
> Authorization: Basic c2hhcGV1cGxvYWRlcjo2OHJpOURwazdJR001bEo=
> User-Agent: curl/7.63.0
> Accept: */*
> Content-type: application/zip
> Content-Length: 7022252
> Expect: 100-continue
>
< HTTP/1.1 100
* We are completely uploaded and fine
< HTTP/1.1 201
< X-Frame-Options: SAMEORIGIN
< Content-Length: 0
< Date: Sat, 23 Mar 2019 23:04:27 GMT
* Connection #0 to host 172.16.17.86 left intact
将 curl 生成的原始 HTTP 数据与您的代码生成的数据进行比较后,我发现了以下差异:
- curl 不生成多部分请求
- curl 生成一个
Expect: 100-continue
header - curl 使用抢占式基本身份验证
以下代码应该等同于该 curl 命令,但我无法使用 GeoServer 对其进行测试。我用过fluent API of apache-httpclient (that is fluent-hc-4.5.7.jar
)。让我知道它是否有效。
import java.io.File;
import org.apache.http.client.fluent.*;
import org.apache.http.entity.ContentType;
public class QuickStart {
public static void main(String[] args) throws Exception {
File file = new File("data.zip");
Executor executor = Executor.newInstance()
.auth("admin", "geoserver")
.authPreemptive("172.16.17.86:9090");
String response = executor.execute(Request.Put("http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/Murrindindi/file.shp")
.useExpectContinue()
.bodyFile(file, ContentType.create("application/zip")))
.returnResponse()
.toString();
System.out.println(response);
}
}