我是网络服务的新手。我试图从一个Android应用程序发送一个InputStream到一个像Rest框架一样使用Jersey 2.22的服务器,但我得到415不支持的媒体类型错误。这是我的客户端代码(我使用Loopj的AsyncHttpClient):
client.addHeader("Content-Type", "application/octet-stream");
final RequestParams requestParams = new RequestParams();
requestParams.put("file", myInputStream);
client.post("MyUrl", requestParams, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
System.out.println("Success");
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
if(responseBody != null)
System.out.println(new String(responseBody, StandardCharsets.UTF_8));
System.out.println(error.getMessage());
System.out.println("Failure");
}
});
服务器端代码是:
@Path("/schedule")
public class schedule {
@POST
@Secured
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response getScenario(@FormDataParam("file")InputStream payload){
System.out.println("I'm in");
//Do stuff with payload
return Response.ok().build();
}
但如果不是@FormDataParam,我只会把
public Response getScenario(InputStream payload)
我收到的inputStream没有任何415错误(当然在这种情况下inputStream也包含httpBody)。
在网上搜索我已阅读有关依赖性问题,但一切似乎都没问题。这是我的pom.xml和资源配置:
final ResourceConfig rc = new ResourceConfig().packages("MyPackages", "org.glassfish.jersey.examples.multipart");
rc.register(MultiPartFeature.class);
的pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
<!-- uncomment this to get JSON support: -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
</dependency>
<dependency>
<groupId>org.jvnet.mimepull</groupId>
<artifactId>mimepull</artifactId>
<version>1.9.6</version>
</dependency>
<properties>
<jersey.version>2.22.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
答案 0 :(得分:0)
我解决了只是从@Consumes(MediaType.APPLICATION_OCTET_STREAM)更改@Consumes注释的问题 至 @Consumes(MediaType.MULTIPART_FORM_DATA)
并从客户端http请求中删除标头