I've been using a solution that I found by searching the struts user
archive to display image data from the database.

I've developed a page that displays at most two images on one page (one
is referred as the "global" image and the other the "local" image in the
code shown below).

Most of the time the correct images are displayed, however, sometimes
the same image is displayed for both "global" and "local" but the images
stored are actually different. There is an inconsistency. Re-displaying
the page sometimes shows the same image and sometimes different images.
Any ideas?


Here is parts of interface
WebDefs:****************************************************************
***************************

        public static final String PARAM_IS_GLOBAL_IMAGE =
"isGlobalImage";
        public static final String PARAM_GLOBAL_ID = "globalId";
        public static final String PARAM_LOCAL_ID = "localId";


Here is the main action
class:******************************************************************
*************************

public class LoadCampaignImageAction extends LoadImageAction implements
WebDefs {

    /**
     * @param mapping The ActionMapping used to select this instance
     * @param form The ActionForm bean for this request
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     * @return ActionForward
     * @throws Exception
     * @see
com.ford.focit.web.BaseAction#executeAuthorized(ActionMapping,
ActionForm, HttpServletRequest, HttpServletResponse)
     */
    public ActionForward executeAuthorized(ActionMapping mapping,
 
ActionForm form,
 
HttpServletRequest request,
 
HttpServletResponse response) throws Exception {

        trace(" -- entered " + this.getClass().getName() +
".executeAuthorized(mapping, form, request, response)");
        
        String isGlobalImageParam =
request.getParameter(PARAM_IS_GLOBAL_IMAGE);
        String globalIdParam = request.getParameter(PARAM_GLOBAL_ID);
        String localIdParam = request.getParameter(PARAM_LOCAL_ID);

                ConsumerCampaignImageDTO imageDTO = null;

        try {
                ConsumerCampaignService ccsService = new
ConsumerCampaignService(super.getIsisUserContext(request));
                if (globalIdParam != null) {
                                ConsumerCampaignGlobalPKDTO
globalCampaignPK = new ConsumerCampaignGlobalPKDTO();
                                globalCampaignPK.setGlobalCampaignId(new
Integer(globalIdParam));
        
globalCampaignPK.restoreFromVersionedString(globalCampaignPK.toVersioned
String());
                                ConsumerCampaignGlobalDTO globalDTO =
ccsService.getGlobalCampaign(globalCampaignPK);
                                if (isGlobalImageParam.equals("true")) {
                                        imageDTO =
globalDTO.getGlobalImage();
                                } else {
                                        imageDTO =
globalDTO.getMasterLocalRecord().getImage();
                                }
                } else if (localIdParam != null &&
isGlobalImageParam.equals("false")) {
                                ConsumerCampaignInfoPKDTO
localCampaignPK = new ConsumerCampaignInfoPKDTO();
                                localCampaignPK.setCampaignId(new
Integer(localIdParam));
        
localCampaignPK.restoreFromVersionedString(localCampaignPK.toVersionedSt
ring());
                                ConsumerCampaignLocalDTO localDTO =
ccsService.getLocalCampagin(localCampaignPK);
                                imageDTO = localDTO.getImage();
                }
                } catch (IsisObjectNotFoundException e) {
                Object[] params = null;
                if (globalIdParam != null) {
                                warning(this.getClass().getName() + "
IsisObjectNotFoundException occurred calling
ConsumerCampaignService.getGlobalCampaign()");
                        params = new Object[] {
getResources(request).getMessage("message.type.campaignGlobal") ,
globalIdParam };
                } else if (localIdParam != null &&
isGlobalImageParam.equals("false")) {
                                warning(this.getClass().getName() + "
IsisObjectNotFoundException occurred calling
ConsumerCampaignService.getLocalCampagin()");
                        params = new Object[] {
getResources(request).getMessage("message.type.campaignLocal") ,
localIdParam };
                }
                ActionErrors actionErrors = new ActionErrors();
                        handleObjectNotFoundException(request,
actionErrors, "error.staleDataArg2", params);
                saveErrors(request, actionErrors);
                return
(mapping.findForward(FORWARD_CAMPAIGN_NOT_FOUND));
        }
        
        if (imageDTO != null) {
                super.setImage(imageDTO.getImage());
                super.setImageType(imageDTO.getImageType());
        }

        return (super.executeAuthorized(mapping, form, request,
response));
    }
}


Here is the abstract superclass that serves the
images:*****************************************************************
**************************

/**
 * 
 *
 */
public abstract class LoadImageAction extends IsisBaseAction {

    /**
     * .
     */
        private byte[] image = null;

    /**
     * .
     */
        private String imageType = null;

    /**
     * @param mapping The ActionMapping used to select this instance
     * @param form The ActionForm bean for this request
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     * @return ActionForward
     * @throws Exception
     * @see
com.ford.focit.web.BaseAction#executeAuthorized(ActionMapping,
ActionForm, HttpServletRequest, HttpServletResponse)
     */
    public ActionForward executeAuthorized(ActionMapping mapping,
 
ActionForm form,
 
HttpServletRequest request,
 
HttpServletResponse response) throws Exception {

        trace(" -- entered " + this.getClass().getName() +
".executeAuthorized(mapping, form, request, response)");
        
                try {
                        final String prefix = "image";
                        final String suffix = null;
                        File file = File.createTempFile(prefix, suffix);
                        FileOutputStream fileOutputStream = new
FileOutputStream(file);
                        fileOutputStream.write(getImage());
                        fileOutputStream.close();
                        response.setContentType(getImageType());
                        response.setHeader("Content-Disposition",
"filename=" + file.getName());
                        response.setHeader("Cache-Control", "no-cache");
                        response.setContentLength((int)file.length());
                        OutputStream outputStream =
response.getOutputStream();
                        FileInputStream fileInputStream = new
FileInputStream(file);
                        byte[] buffer = new byte[(int)file.length()];
                        fileInputStream.read(buffer);
                        outputStream.write(buffer);
                        outputStream.flush();
                        outputStream.close();
                        fileInputStream.close();
                        file.delete();
                        file.deleteOnExit();
                } catch (IOException ioe) {
                trace(this.getClass().getName() + "==>> " +
ioe.toString());
                }

        return null;
    }

    /**
     * Returns the image.
     * @return byte[]
     */
    private byte[] getImage() {
        return image;
    }

    /**
     * Sets the image.
     * @param image The image to set
     */
    protected void setImage(byte[] image) {
        this.image = image;
    }

    /**
     * Returns the imageType.
     * @return String
     */
    private String getImageType() {
        return imageType;
    }

    /**
     * Sets the imageType.
     * @param imageType The imageType to set
     */
    protected void setImageType(String imageType) {
        this.imageType = imageType;
    }
}


Here are snippets of the
JSP:********************************************************************
***********************

<%-- DISPLAY GLOBAL IMAGE --%>

<tr>
        <td align="left">
                <bean:message key="previewLocalCampaign.imageGlobal"
bundle="<%=IConstant.PAGE_RESOURCES_KEY%>"/>
        </td>
        <td align="left">
                <bean:define id="globalIdValue"
name="previewLocalCampaignForm" property="globalId"
type="java.lang.String"/>
                <bean:define id="imageWidthGlobalValue"
name="previewLocalCampaignForm" property="imageWidthGlobal"
type="java.lang.String"/>
                <bean:define id="imageHeightGlobalValue"
name="previewLocalCampaignForm" property="imageHeightGlobal"
type="java.lang.String"/>
                <%
                        map = new HashMap();
                        map.put("globalId", globalIdValue);
                        map.put("isGlobalImage","true");
                        pageContext.setAttribute("map", map,
PageContext.PAGE_SCOPE);
                %>
                <html:img styleId="imagePreviewGlobal"
page="/loadCampaignImage.do" name="map"
width="<%=imageWidthGlobalValue%>"
height="<%=imageHeightGlobalValue%>"/>
        </td>
</tr>

<%-- DISPLAY LOCAL IMAGE --%>

<tr>
        <td align="left">
                <bean:message key="previewLocalCampaign.imageLocal"
bundle="<%=IConstant.PAGE_RESOURCES_KEY%>"/>
        </td>
        <td align="left">
                <bean:define id="localIdValue"
name="previewLocalCampaignForm" property="localId"
type="java.lang.String"/>
                <bean:define id="imageWidthLocalValue"
name="previewLocalCampaignForm" property="imageWidthLocal"
type="java.lang.String"/>
                <bean:define id="imageHeightLocalValue"
name="previewLocalCampaignForm" property="imageHeightLocal"
type="java.lang.String"/>
                <%
                        map = new HashMap();
                        map.put("localId", localIdValue);
                        map.put("isGlobalImage","false");
                        pageContext.setAttribute("map", map,
PageContext.PAGE_SCOPE);
                %>
                <html:img styleId="imagePreviewLocal"
page="/loadCampaignImage.do" name="map"
width="<%=imageWidthLocalValue%>" height="<%=imageHeightLocalValue%>"/>
        </td>
</tr>

Thanks in advance,

Christina Siena

Reply via email to