本文介绍了JAX-RS接受图像作为输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间以来,我一直在为我的开发需求开发JAX-RS Web服务。到目前为止我写的所有方法都接受java字符串或原始类型作为输入。

For quite some time now, I've been developing JAX-RS web services for my development needs. All the methods that I've written so far accept java Strings or primitive types as input.

这种方法的一个例子:

@POST  
@Path("MyMethod")  
@Produces(MediaType.APPLICATION_JSON)  
public String MyMethod(@FormParam("username")String username, @FormParam("password")String passowrd)

我是什么现在尝试做的是接受图像作为输入。我读了很多关于此的文章。有人建议接受base64编码作为输入,其他建议接受实际的InputSteam。

What I'm trying to do now is accept images as input. I read a lot of articles regarding this. Some suggested accepting the base64 encoding as input and others suggested accepting an actual InputSteam.

但是,我还没有看到关于如何接受InputStream的完整示例。我读到了@consumer注释和@Provider,但我还是无法绕过它。是否有文章,文档或示例以某种方式引导我走向这个?关于如何实现而不是显示理论的一步一步的过程。

However, i'm yet to see a full blown example on how to accept an InputStream. I read about the @consumer annotation and @Provider but i still can't wrap my head around it. Is there an article, documentation or an example that somehow guides me toward this? i.e. A step by step process on how to implement rather than displaying theory.

我知道base64编码有效,但出于好奇,我想知道另一种方法同样有效...提前致谢。

I know that the base64 encoding works but out of curiosity i would like to know how the other approach works as well...Thanks in advance.

推荐答案

这应该有效:

import org.apache.commons.io.IOUtils;
@POST
@Path("MyMethod") 
@Consumes("*/*") // to accept all input types 
public String MyMethod(InputStream stream) {
    byte[] image = IOUtils.toByteArray(stream);
    return "done";
}

这篇关于JAX-RS接受图像作为输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 09:44