Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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-如何从RESTful web服务发送图像?_Java_Image_Rest_Server - Fatal编程技术网

java-如何从RESTful web服务发送图像?

java-如何从RESTful web服务发送图像?,java,image,rest,server,Java,Image,Rest,Server,我正在Java上实现一个无框架的Restful Web服务。我的想法是从文件服务器发送图像,因为将图像存储在数据库中会降低服务器的速度。目前,我有以下代码,返回json内容: @Path("articulo") public class ArticuloResource { @Context private UriInfo context; private final ArticuloService service; private final Gson gso

我正在Java上实现一个无框架的Restful Web服务。我的想法是从文件服务器发送图像,因为将图像存储在数据库中会降低服务器的速度。目前,我有以下代码,返回json内容:

@Path("articulo")
public class ArticuloResource {

    @Context
    private UriInfo context;
    private final ArticuloService service;
    private final Gson gson;

    /**
     * Creates a new instance of ArticuloResource
     */
    public ArticuloResource() {
        this.service = new ArticuloService();
        this.gson = new Gson();
    }

    /**
     * Retrieves representation of an instance of ArticuloResource
     * @return an instance of com.tienda.rest.pojo.Articulo
     */
    @GET
    @Produces(Metodos.Parametros.TYPE_APPLICATION_JSON)
    public String getJson() {
        return this.gson.toJson(this.service.seleccionarTodo());
    }    

    @GET
    @Path("novedades")
    @Produces(Metodos.Parametros.TYPE_APPLICATION_JSON)
    public String getNovedades() {
        return "Novedades";
    }

    @GET
    @Path("{id}")
    @Produces(Metodos.Parametros.TYPE_APPLICATION_JSON)
    public String getArticulo(@PathParam("id") Integer id) {
        return this.gson.toJson(this.service.getUnique(id));
    }

    /**
     * PUT method for updating or creating an instance of ArticuloResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes(Metodos.Parametros.TYPE_APPLICATION_JSON)
    public void putJson(Articulo content) {
    }

    @GET
    @Produces(Metodos.Parametros.TYPE_IMAGE_PNG)
    public Response getImage() {
        return null;
    }
}
想法?提前感谢。

尝试以下内容:

  @GET
    @Produces(Metodos.Parametros.TYPE_IMAGE_PNG)
    public Response getImage() {
        byte[] bytes = Files.toByteArray("file.png");
        return Response.ok(bytes).build();
    }

您可以尝试对图像进行流式处理。也许会好一点<代码>返回响应.ok(新的ByteArrayInputStream(字节)).build()


然而,无论你选择哪个选项,它都会有点慢。您可以向另一台服务器发送重定向链接,该服务器可以独立于您的应用程序服务器向客户端交付图像。这比发送图像本身作为响应要好。

有一个解决办法,但这取决于存储图像所使用的文件服务器

如果是windows文件服务器或S3,则可以从REST服务返回映像的链接。在html页面中,可以使用
。但是(对于windows文件服务器),它在DMZ中工作,而不在客户端的外部n/w中工作

若应用程序要暴露于外部世界(DMZ之外),那个么REST服务应该吐出字节数组。
顺便说一句,这不会使您的服务器速度急剧降低。

这就是问题所在。发送字节数组会降低服务器速度。这与将图像存储在数据库中并通过字节数组发送到客户端是一样的。没有其他方法发送文件本身?您可以尝试流式传输图像。也许会好一点<代码>返回响应.ok(新的ByteArrayInputStream(字节)).build()不会
返回响应。ok(新文件输入流(新文件(“File.png”)))
会更好(更快),因为它不需要先在内存中缓存整个文件内容,但允许文件的直接流?