使用 Restlet 提供动态生成的图像

发布于 2024-08-07 09:16:40 字数 260 浏览 5 评论 0原文

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

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

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

谢谢

I have searched online for a while and almost all the questions regarding image serving using restlet are about static images. What I want to do is to serve dynamic generated image from restlet.

I have tried serving static images using restlet, it's working. Also, I can successfully generate a dynamic image and store them in a local folder, so the problem goes to how to serving it. If it's an http response, what I shall do is to attach all the bytes of the image to the body of the response. However, I am not sure about how to use restlet to do that? Is it FileRepresentation?

Newbie in this field, and any suggestion will be welcomed.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一抹淡然 2024-08-14 09:16:41

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

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

希望这有帮助! :)

I'm a little late to the party, but here is a class with which you can serve your images:

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

}

In the restlet handler, you can then return it like this:

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

Hope this helps! :)

情域 2024-08-14 09:16:41

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

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

There is an easyer way you can use ByteArrayRepresentation:

@Get
public ByteArrayRepresentation getThumbnail() {
    byte[] image = this.getImage();
    return new ByteArrayRepresentation(image , MediaType.IMAGE_PNG);
}
鸠魁 2024-08-14 09:16:41

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

FileRepresentation should work if you write the image to a file first. For a more efficient approach, you can create your own Representation class by extending OutputRepresentation and overriding the write(OutputStream) method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文