【发布时间】:2014-11-06 16:09:38
【问题描述】:
我正在尝试编写一个服务,该服务将通过将文件作为 POST 实体中的字节数组来负责上传文件。这是我的代码
我的 CXF 服务
@Path("MyTest")
public class TestService {
@POST
public String MyPost(Byte[] bytes){
System.out.println("Service invoked");
return "Hello, I am a POST response";
}
}
我的客户
File image = new File("C:\\snake.jpg");
FileInputStream is = new FileInputStream(image);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] fileInBytes = bos.toByteArray();
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/MyApp");
target = target.path("MyTest");
Response response = target.request().post(Entity.entity(fileInBytes, MediaType.APPLICATION_OCTET_STREAM));
InputStream i = (InputStream) response.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(i));
System.out.println(br.readLine());
这是我得到的错误
SEVERE: No message body reader has been found for class [Ljava.lang.Byte;, ContentType: application/octet-stream
Nov 06, 2014 4:02:50 PM org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse
WARNING: javax.ws.rs.WebApplicationException: HTTP 415 Unsupported Media Type
at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1298)
...
有什么想法吗?有没有更好的文件上传服务方式?
谢谢
【问题讨论】:
标签: java web-services cxf jax-rs