0

我在将图像从数据库加载到 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>
4

2 回答 2

0

切换到您自己的结果实现 Result 类并在此处写入输出。其他在struts.xml行动结果类型中指定自己的近似匹配参数。参考:生成自己的图像作为动作结果

于 2012-07-27T13:08:07.273 回答
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);
        }
    }
于 2012-07-27T10:22:47.420 回答