我有一个POST请求代码,可以上传文件。它可以正确上载文件,但是失败时会出现“管道破损”,而不是我提供不正确的API密钥时出现的“ 401未经授权”。
我已使用Postman验证提供错误API密钥时我们的服务器是否发送401未经授权。
我的印象是,即使服务器在发送“ 401未经授权”后已经关闭了连接,我的客户端代码仍然尝试向该流发送内容。
这是我正在使用的代码:
try {
return target
.path("storage")
.path("upload")
.request()
.header("Authorization", apiKeyHeader)
.header("App-Type", type)
.post(Entity.entity(Files.newInputStream(file.toPath()), APPLICATION_OCTET_STREAM), String.class);
} catch (IOException e) {
e.printStackTrace(); // <-- exception comes here
}
我还尝试了将Jersey升级到可用的最新版本:2.27
,但这没有任何改变。
javax.ws.rs.ProcessingException: java.net.SocketException: Broken pipe (Write failed)
at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:496)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:278)
at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$1(JerseyInvocation.java:767)
at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
at org.glassfish.jersey.internal.Errors.process(Errors.java:229)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:414)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:765)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:456)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:357)
at org.comcom.rest.api.resource.v2.AppStorageResource.uploadFile(AppStorageResource.java:70)
这是我的pom.xml配置:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-apache-connector</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.23.1</version>
</dependency>