Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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 restful返回映像_Java_Image_Rest_Cxf - Fatal编程技术网

Java cxf restful返回映像

Java cxf restful返回映像,java,image,rest,cxf,Java,Image,Rest,Cxf,我是使用CXF和Spring创建RESTful Web服务的新手。这个项目在maven的控制之下 我有一个android应用程序,它想连接到服务器,通过RESTful Web服务检索图书信息和封面图片。RESTful服务是否可能(以及如何)返回映像 项目浏览器 Project --Java Resource ----src/main/java ------library.service --------IBook.java --------Book.java ----images ------C

我是使用CXF和Spring创建RESTful Web服务的新手。这个项目在maven的控制之下

我有一个android应用程序,它想连接到服务器,通过RESTful Web服务检索图书信息和封面图片。RESTful服务是否可能(以及如何)返回映像

项目浏览器

Project
--Java Resource
----src/main/java
------library.service
--------IBook.java
--------Book.java
----images
------Cover1.png
------Cover2.png
--JavaResources
--Deployed Resources
----webapp
------WEB-INF
----web-resources
IBook.java接口

@GET
@Path("/book/cover/{name}")
@Produces("image/png")
public Image getImage(@PathParam("name") String name);
Book.java impl

public Image getImage(String name){
    //How should I get cover png from 'images' folder and return it??
}
谢谢


Silvester Pang

您可以通过Response.ok(params…)返回所选图像

将您的IBook.java更改为

    @GET
    @Path("/book/cover/{name}")
    @Produces("image/*")
    public Response getImage(@PathParam("name") String name);
以及与此相关的实现接口

public Response getImage(String name){
    File file = new File(fileUrl);
    String mediaType = SomeContentTypeMapHere(file)
    return Response.ok(file,mediaType).build()
}
编辑

我们使用了
Response.ok(对象类型,字符串mediaType).build()
在调用方法处理程序时生成并返回响应。第一个参数是一个对象。把它想象成当某人/某人在给定的url上发送请求时我们要发送的东西,而mediaType基本上是我们要发送的内容类型(例如,“image/png、image/jpg等等)


请记住将您的
@products(“image/*”)
与您通过
ok(对象实体,字符串mediaType
方法)传递的内容类型相匹配。

您可以通过Response.ok(参数…).build()返回所选图像

将您的IBook.java更改为

    @GET
    @Path("/book/cover/{name}")
    @Produces("image/*")
    public Response getImage(@PathParam("name") String name);
以及与此相关的实现接口

public Response getImage(String name){
    File file = new File(fileUrl);
    String mediaType = SomeContentTypeMapHere(file)
    return Response.ok(file,mediaType).build()
}
编辑

我们使用了
Response.ok(Object type,String mediaType).build()
在调用方法处理程序时生成并返回响应。第一个参数是一个对象。当某人/某人在给定url上发送请求,而mediaType基本上是内容类型(例如,“我们将要发送的内容的image/png、image/jpg等)


请记住将您的
@products(“image/*”)
与您通过
ok(Object entity,String mediaType
方法传递的内容类型相匹配。

我不太懂Response的用法。您介意解释一下吗?我不太懂Response的用法。您介意解释一下吗?