请在下面找到发件人类别
private static CloseableHttpClient httpClient() {
try {
TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", sf)
.build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
connectionManager.setMaxTotal(maxTotalConnections);
connectionManager.setDefaultMaxPerRoute(maxConnectionsPerRoute);
CloseableHttpClient defaultHttpClient = HttpClientBuilder.create()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(RequestConfig.DEFAULT)
.build();
return defaultHttpClient;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
static String authHeaders(String username, String password ){
String auth = username + ":" + password;
String authHeader = "Basic " + auth;
return authHeader;
}
public static void main(String[] args) {
try{
CloseableHttpClient httpClient = httpClient();
HttpPost postRequest = new HttpPost("https://localhost:8018/receiver?filename=data.tar.gz");
//postRequest.addHeader(HttpHeaders.TRANSFER_ENCODING, "chunked");
postRequest.addHeader(HttpHeaders.AUTHORIZATION, authHeaders("test","test") );
postRequest.addHeader(HttpHeaders.CONTENT_ENCODING, "gzip");
File file = new File(Receiver.class.getClassLoader().getResource("data.tar.gz").getFile());
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addPart("data.tar.gz", new FileBody(file, "application/x-gzip"));
postRequest.setEntity(builder.build());
// MultipartEntity multipart = new MultipartEntity();
// ContentBody fileContent = new FileBody(file); //For tar.gz: "application/x-gzip"
// multipart.addPart("data.tar.gz", fileContent);
// postRequest.setEntity(multipart);
HttpResponse response = httpClient.execute(postRequest);
System.out.println(response);
} catch (Exception e) {
e.printStackTrace();
}
}
我正在尝试通过http客户端发布gzip文件,并且出现以下错误
com.filter.GzipFilter|Error on reading gz file:
java.util.zip.ZipException: Not in GZIP format
at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:165)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:79)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:91)
at com.filter.GzipFilter.doFilter(GzipFilter.java:46)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
使用http客户端发布tar.gz文件时出错,但是如果我使用curl命令发布同一文件,它可以正常工作并正确执行,但通过http客户端传递时会出错。
卷曲命令
curl -u test:test --insecure -T ./data.tar.gz -v -X POST --header "Content-Encoding: gzip" 'https://localhost:8018/receiver?filename=data.tar.gz'
请检查并让我知道我所缺少的内容,我尝试进行搜索并获得一些详细信息,并使用相同的内容但尚未获得解决方案来实现。
答案 0 :(得分:0)
那行不通
File file = new File(Receiver.class.getClassLoader().getResource("data.tar.gz").getFile());
资源不是文件
将应用打包到文件中后,您将无法访问该资源。
使用InputStreamBody
代替
InputStream in=Receiver.class.getClassLoader().getResourceAsStream("data.tar.gz");
builder.addPart("data.tar.gz", new InputStreamBody( in,"application/x-gzip"));
这只是示例。请在请求后记住关闭输入流。
答案 1 :(得分:0)
我们需要启用分块解决此问题。
File file = new File(CallHttpReceiver.class.getClassLoader().getResource(fileName).getFile());
FileEntity fileEntity = new FileEntity(file);
fileEntity.setChunked(true);
postRequest.setEntity( fileEntity );
我们需要将我们的fileEntity设置为分块,以将gzip作为请求传递,并且对我有用。感谢所有支持并尝试为我提供成果的人。