Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java CXF“;未找到邮件正文读取器“;用于POST服务中的字节数组参数_Java_Web Services_Cxf_Jax Rs - Fatal编程技术网

Java CXF“;未找到邮件正文读取器“;用于POST服务中的字节数组参数

Java CXF“;未找到邮件正文读取器“;用于POST服务中的字节数组参数,java,web-services,cxf,jax-rs,Java,Web Services,Cxf,Jax Rs,我正在尝试编写一个服务,该服务将负责上传文件,方法是将文件作为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"; }

我正在尝试编写一个服务,该服务将负责上传文件,方法是将文件作为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)
...
有什么想法吗?有没有更好的方法来提供文件上传服务


谢谢

您可以使用
InputStream

@POST
public String MyPost(InputStream is) throws IOException  {
    BufferedImage image = ImageIO.read(is);
    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(image)));
    System.out.println("Service invoked");
    return "Hello, I am a POST response";
}
另外,在客户机代码中,您实际上没有向输出流写入任何内容。它应该更像

FileInputStream is = new FileInputStream(image);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

int nRead;
byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1) {
    buffer.write(data, 0, nRead);
}
buffer.flush();
byte[] fileInBytes = buffer.toByteArray();

Response response = target.request().post(Entity.entity(fileInBytes, 
        MediaType.APPLICATION_OCTET_STREAM));

更新

实际上,您可以使用字节数组。您只需要使用原语
byte[]

@POST
public String MyPost(byte[] bytes) throws IOException  {
    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bytes)));
    System.out.println("Service invoked");
    return "Hello, I am a POST response";
}

甚至可以使用
文件

@POST
public String MyPost(File file) throws IOException  {
    BufferedImage image = ImageIO.read(file);
    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(image)));
    System.out.println("Service invoked");
    return "Hello, I am a POST response";
}
JAX-RS为磁盘上的输入创建一个临时文件。它从网络缓冲区读取数据,并将读取的字节保存到此临时文件中


他们都工作。选择你的毒药

也有同样的问题。将application/octet stream作为参数添加到@Consumes注释解决了以下问题:

@POST
@Consumes({"application/octet-stream", "text/xml", "application/xml"})
@Produces({"text/xml", "application/xml"})
public Object post(String xmlData) throws Exception {
    ...
}
但是,如果您可以确保调用者在HTTP请求头中设置了适当的内容类型,则可能不需要向@Consumes注释添加“应用程序/八位字节流”

使用CURL的示例如下:

curl-vX POST-H“内容类型:text/xml”-T“postdata.xml”“…”