拥有ResourceHandler以从数据库流式传输图像 [英] Own ResourceHandler to stream images from DB

查看:189
本文介绍了拥有ResourceHandler以从数据库流式传输图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力实现自己的资源. getInputStream 方法不会被调用.

我的处理程序:

I'm strugging with my own resource-implementation. The getInputStream-method doesn't get called.

My handler:

public class ResourceHandlerWrapperImpl extends
        ResourceHandlerWrapper {

  private final ResourceHandler wrapped;

  public ResourceHandlerWrapper(final ResourceHandler wrapped)
  {
    this.wrapped = wrapped;
  }

  @Override
  public ResourceHandler getWrapped()
  {
    return wrapped;
  }

  @Override
  public Resource createResource(final String resourceName, final String libraryName)
  {
    if (AppConstants.RESOURCE_MEDIA_LIB.equals(libraryName))
    {
      return new MediaResource(resourceName);
    }
    else
    {
      return super.createResource(resourceName, libraryName);
    }
  }

  /**
   * @see javax.faces.application.ResourceHandlerWrapper#libraryExists(java.lang.String)
   */
  @Override
  public boolean libraryExists(final String libraryName)
  {
    if (AppConstants.RESOURCE_MEDIA_LIB.equals(libraryName))
    {
      return true;
    }
    else
    {
      return super.libraryExists(libraryName);
    }
  }

  /**
   * @see javax.faces.application.ResourceHandlerWrapper#isResourceRequest(javax.faces.context.FacesContext)
   */
  @Override
  public boolean isResourceRequest(final FacesContext context)
  {
    return super.isResourceRequest(context);
  }

}

我的资源实现:

My resource implementation:

public class MediaResource extends Resource {

    private final String mediaId;

    public MediaResource(final String mediaId) {
        setLibraryName(AppConstants.RESOURCE_MEDIA_LIB);
        setResourceName(mediaId);
        setContentType("image/png");
        this.mediaId = mediaId;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        if (mediaId != null) {
            System.out.println("Yeahhh!!!");
        }

        return null;
    }

    @Override
    public Map<String, String> getResponseHeaders() {
        return new HashMap<String, String>();
    }

    @Override
    public String getRequestPath() {
        final FacesContext context = FacesContext.getCurrentInstance();
        return context
                .getApplication()
                .getViewHandler()
                .getResourceURL(
                        context,
                        ResourceHandler.RESOURCE_IDENTIFIER + "/" + mediaId
                                + "?ln=" + AppConstants.RESOURCE_MEDIA_LIB);
    }

    @Override
    public URL getURL() {
        return null;
    }

    @Override
    public boolean userAgentNeedsUpdate(final FacesContext context) {
        return true;
    }

}

在我的faces-config.xml中:

In my faces-config.xml:

<application>
    <resource-handler>com.foo.bbb.ResourceHandlerWrapperImpl</resource-handler>
</application>

在我的jsf中:

In my jsf:

<h:graphicImage library="media_lib" name="66" width="50" />

以html格式输出:

Output in html:

<img src="/foo/javax.faces.resource/66?ln=media_lib" width="50" />


从getRequestPath返回:/foo/javax.faces.resource/66?ln=media_lib

MediaResource被调用并初始化,但没有调用getInputStream. FireBug在此URL上显示404(称为两次). 我完全不知道我在做什么错.

MediaResource is called and initialized, but the getInputStream isn't called. FireBug shows a 404 on this url (called twice). I'm totally puzzled what I'm doing wrong here.

谢谢
强尼

推荐答案

发现了错误.我的rescource实现的 getRequestPath 是错误的.我忘记了在URL中将faces-mapping( Util.getFacesMapping(context))到faces-servlet:

found the mistake .The getRequestPath of my rescource implementation was faulty. I forgot the faces-mapping (Util.getFacesMapping(context)) to the faces-servlet in the url:

@Override
    public String getRequestPath() {
        final FacesContext context = FacesContext.getCurrentInstance();
        return context
                .getApplication()
                .getViewHandler()
                .getResourceURL(
                        context,
                        ResourceHandler.RESOURCE_IDENTIFIER + "/" + mediaId + Util.getFacesMapping(context)
                                + "?ln=" + AppConstants.RESOURCE_MEDIA_LIB);

现在一切正常.

感谢BalusC的帮助.

Thanks to BalusC for his help.

干杯
强尼

这篇关于拥有ResourceHandler以从数据库流式传输图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆