7

我在网上搜索了一段时间,几乎所有关于使用 restlet 提供图像服务的问题都是关于静态图像的。我想要做的是从restlet 提供动态生成的图像。

我曾尝试使用 restlet 提供静态图像,它正在工作。此外,我可以成功生成动态图像并将它们存储在本地文件夹中,所以问题在于如何提供它。如果是 http 响应,我要做的就是将图像的所有字节附加到响应的正文中。但是,我不确定如何使用 restlet 来做到这一点?是文件表示吗?

该领域的新手,欢迎提出任何建议。

谢谢

4

3 回答 3

5

我参加聚会有点晚了,但是这里有一个课程,您可以使用它来提供图像:

package za.co.shopfront.server.api.rest.representations;

import java.io.IOException;
import java.io.OutputStream;

import org.restlet.data.MediaType;
import org.restlet.representation.OutputRepresentation;

public class DynamicFileRepresentation extends OutputRepresentation {

    private byte[] fileData;

    public DynamicFileRepresentation(MediaType mediaType, long expectedSize, byte[] fileData) {
        super(mediaType, expectedSize);
        this.fileData = fileData;
    }

    @Override
    public void write(OutputStream outputStream) throws IOException {
        outputStream.write(fileData);
    }

}

在 restlet 处理程序中,您可以像这样返回它:

@Get
public Representation getThumbnail() {

    String imageId = getRequest().getResourceRef().getQueryAsForm().getFirstValue("imageId");
    SDTO_ThumbnailData thumbnailData = CurrentSetup.PLATFORM.getImageAPI().getThumbnailDataByUrlAndImageId(getCustomerUrl(), imageId);
    return new DynamicFileRepresentation(
            MediaType.valueOf(thumbnailData.getThumbNailContentType()), 
            thumbnailData.getSize(), 
            thumbnailData.getImageData());
}

希望这可以帮助!:)

于 2010-09-07T11:43:26.853 回答
3

有一种更简单的方法可以使用ByteArrayRepresentation

@Get
public ByteArrayRepresentation getThumbnail() {
    byte[] image = this.getImage();
    return new ByteArrayRepresentation(image , MediaType.IMAGE_PNG);
}
于 2013-07-31T06:26:08.277 回答
0

如果您先将图像写入文件,则 FileRepresentation 应该可以工作。要获得更有效的方法,您可以通过扩展 OutputRepresentation 并覆盖该方法来创建自己的 Representation 类write(OutputStream)

于 2009-12-02T16:41:48.227 回答