【问题标题】:Serve dynamic generated images using restlet使用 restlet 提供动态生成的图像
【发布时间】:2009-10-02 14:29:11
【问题描述】:

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

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

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

谢谢

【问题讨论】:

  • 那么你有什么尝试?你可以提供静态图像吗?真正的问题是您无法生成动态图像(不是它的服务吗?)

标签: java image dynamic restlet


【解决方案1】:

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

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());
}

希望这会有所帮助! :)

【讨论】:

    【解决方案2】:

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

    @Get
    public ByteArrayRepresentation getThumbnail() {
        byte[] image = this.getImage();
        return new ByteArrayRepresentation(image , MediaType.IMAGE_PNG);
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2010-11-07
        • 1970-01-01
        • 2014-05-18
        • 2018-04-04
        • 1970-01-01
        • 1970-01-01
        • 2011-03-14
        • 2012-09-05
        • 1970-01-01
        相关资源
        最近更新 更多