7

しばらくオンラインで検索しましたが、restlet を使用した画像提供に関するほとんどすべての質問は、静的画像に関するものです。私がやりたいことは、restlet から動的に生成された画像を提供することです。

restlet を使用して静止画像を提供しようとしましたが、機能しています。また、動的な画像を正常に生成してローカル フォルダーに保存できるので、問題はそれを提供する方法です。http 応答の場合は、画像のすべてのバイトを応答の本文に添付します。ただし、restletを使用してそれを行う方法がわかりませんか? FileRepresentationですか?

この分野の初心者であり、どんな提案も歓迎されます。

ありがとう

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を拡張し、write(OutputStream)メソッドをオーバーライドすることで、独自のRepresentationクラスを作成できます。

于 2009-12-02T16:41:48.227 に答える