从数据库将图像加载到jsp(struts2)

时间:2012-07-26 14:26:29

标签: database image jsp struts2

我在将数据库从数据库加载到jsp视图时遇到问题。看起来行动是执行但停在中间。 我在execute()方法启动时和结束时记录。在日志文件中,我可以看到启动此方法,但没有结束。

我有行动:

public class ShowImageAction extends BaseAction {

    private static final long serialVersionUID = 1L;

    private static final Log LOG = LogFactory.getLog(ShowImageAction.class);

    private int id;

    public String execute() {
        LOG.info("Enter execute()");
        MessageBean messageBean = (MessageBean) sessionParameters
                .get(SessionParameters.BANNER_EDIT);

        IFiles filesEJB = EJBLocator.getFiles();
        if (messageBean != null) {
            id = messageBean.getBannerId();
        }

        FileBean file = filesEJB.file(id);

        LOG.info("Id = " + id);
        if (file != null) {
            byte[] bytes = null;
            try {
                LOG.info("File: name = " + file.getName() + ". type = "
                        + file.getType());
                if (file.getImage() != null) {
                    bytes = FileUtils.readFileToByteArray(file.getImage());
                    HttpServletResponse response = ServletActionContext
                            .getResponse();
                    response.setContentType(file.getType());
                    OutputStream out = response.getOutputStream();
                    out.write(bytes);
                    out.close();
                } else {
                    LOG.info("execute(): Nie udało się pobrać pliku z bazy danych!");
                }
            } catch (IOException e) {
                LOG.error(e);
            }
        } else {
            LOG.info("execute(): Nie udało się pobrać pliku z bazy danych!");
        }
        LOG.info("Exit execute()");
        return NONE;
    }

JSP:

    <div class="OneFilteringRow">
        Podgląd<br />
        <img src="/ebok-bm/ShowImageAction" alt="Podgląd banera" />
    </div>

struts.xml中

    <action name="ShowImageAction"
            class="pl.bm.action.content.ShowImageAction">
    </action>

1 个答案:

答案 0 :(得分:0)

问题在于:

FileBean file = filesEJB.file(id);

我从数据库中获取字节并尝试将其保存到tmpFile。现在我获取字节并将它们写入临时文件并立即将其传输到操作,一切正常。 现在我有:

    FileBean file = filesEJB.file(id);

    LOG.info("Id = " + id);
    if (file != null) {
        byte[] bytes = null;
        try {
            LOG.info("File: name = " + file.getName() + ". type = "
                    + file.getType());
            if (file.getImage() != null) {
                **bytes = file.getImage();**
                HttpServletResponse response = ServletActionContext
                        .getResponse();
                response.setContentType(file.getType());
                OutputStream out = response.getOutputStream();
                out.write(bytes);
                out.close();
            } else {
                LOG.info("execute(): Nie udało się pobrać pliku z bazy danych!");
            }
        } catch (IOException e) {
            LOG.error(e);
        }
    }