Java 对等方重置连接:套接字写入错误httpclient和geoserver

Java 对等方重置连接:套接字写入错误httpclient和geoserver,java,apache-httpclient-4.x,geoserver,Java,Apache Httpclient 4.x,Geoserver,我需要通过REST将压缩的shapefile上传到。GeoServer提供了一个示例CURL来上载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

我需要通过REST将压缩的shapefile上传到。GeoServer提供了一个示例CURL来上载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命令工作正常。但是,我想在JavaHttpClient中进行此上传。我是Java新手,但我编写了以下代码来实现这一点:

import java.io.FileInputStream;
导入java.io.InputStream;
导入org.apache.http.HttpEntity;
导入org.apache.http.auth.AuthScope;
导入org.apache.http.auth.UsernamePasswordCredentials;
导入org.apache.http.client.CredentialsProvider;
导入org.apache.http.client.methods.CloseableHttpResponse;
导入org.apache.http.client.methods.HttpPut;
导入org.apache.http.entity.ContentType;
导入org.apache.http.entity.mime.MultipartEntityBuilder;
导入org.apache.http.impl.client.BasicCredentialsProvider;
导入org.apache.http.impl.client.CloseableHttpClient;
导入org.apache.http.impl.client.HttpClients;
导入org.apache.http.util.EntityUtils;
公共类快速入门{
公共静态void main(字符串[]args)引发异常{
String file=“data.zip”;
CredentialsProvider credsProvider=新的BasicCredentialsProvider();
credsProvider.setCredentials(
任何,
新用户名密码凭据(“管理员”、“地理服务器”);
CloseableHttpClient httpclient=HttpClients.custom()
.setDefaultCredentialsProvider(CredProvider)
.build();
试一试{
HttpPut HttpPut=新HttpPut(“http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/Murrindindi/file.shp");
File file2=新文件(“data.zip”);
InputStream InputStream=新文件InputStream(文件);
HttpEntity reqEntity=MultipartEntityBuilder.create()
.addPart(“文件”,新的文件体(file2,ContentType.DEFAULT_BINARY))
.addBinaryBody(“上游”、inputStream、ContentType.create(“应用程序/zip”)、“data.zip”)
.build();
httpPut.setEntity(请求实体);
System.out.println(“正在执行请求”+httpPost.getRequestLine());
CloseableHttpResponse response=httpclient.execute(httpPut);
试一试{
System.out.println(“--------------------------------------------------------”;
System.out.println(response.getStatusLine());
HttpEntity当前性=response.getEntity();
if(最近性!=null){
System.out.println(“响应内容长度:+resEntity.getContentLength());
}
实体效用消耗(最近);
}最后{
response.close();
}
}最后{
httpclient.close();
}
}

}
将curl生成的原始HTTP数据与代码生成的原始HTTP数据进行比较后,我发现了以下差异:

  • curl不生成多部分请求
  • curl生成一个
    Expect:100 continue
  • curl使用抢占式基本身份验证
下面的代码应该与curl命令等效,但是我不能用GeoServer测试它。我使用了ApacheHttpClient的。让我知道它是否有效

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);
    }

}

只是一个提示,但是curl命令使用HTTPPUT方法,Java代码使用HTTPPOST。这两件事是不同的,您应该始终使用正确的一件。谢谢,我修改了Java代码以使用HttpPut,但仍然得到相同的错误。HttpPut-httpPost=new-HttpPut(“);第二个提示:我认为您应该使用
MultipartEntity
类来发送文件。请看这里这个示例使用post,但您仍然必须使用put。再次感谢,我添加了两行代码:file file2=new file(“data.zip”);.addPart(“file”,new FileBody(file2))希望这是正确的。但我仍然会遇到相同的错误。这非常有效,代码也很简单,谢谢。只发出println语句不会打印任何内容。获取PUT操作的状态会很有用。curl输出显示“Content Length:0,这意味着服务器不响应任何内容。我已经用HTTP结果代码的输出替换了响应主体的输出。