使用Struts2在JSP中不显示上载的图像

时间:2013-12-10 12:37:59

标签: java image jsp struts2

我正在努力使用struts2拦截器显示上传的图像。

我搜索了很多,一些帖子被添加到拦截器删除.tmp文件的上传图像的答案。所以它给出了INFO

  

信息:删除文件userImage   d:\ Workspace.metadata.plugins \ org.eclipse.wst.server.core \ TMP1 \工作\卡塔利娜\本地主机\ E-SchoolWeb \ upload_1c272ff6_142dc6fc692__8000_00000000.tmp

我的图片是成功上传到该位置

  

d:\ Workspace.metadata.plugins \ org.eclipse.wst.server.core \ TMP1 \ wtpwebapps \ E-SchoolWeb

但它没有显示任何图像...只是图像图标显示为好像它是断开的链接。

那么我应该怎样做才能显示该图像。

1)这是我的struts.xml代码

<action name="userImage" class="com.actions.FileUploadAction" method="execute">
    <interceptor-ref name="fileUpload">
    <param name="allowedTypes">image/jpeg,image/png,image/gif</param>
    <param name="maximumSize">4194304</param>
    </interceptor-ref>
    <interceptor-ref name="params" />
    <interceptor-ref name="validation" />
    <interceptor-ref name="workflow" />
    <interceptor-ref name="defaultStack"></interceptor-ref>
    <result name="success" type="redirect">SuccessUserImage.jsp</result>
    <result name="input">userRegister.jsp</result>
</action>

<s:submit value="Upload" align="center" />
</s:form>

2)动作类

private static final long serialVersionUID = 1L;
private File userImage;
private String userImageContentType;
private String userImageFileName;

private HttpServletRequest servletRequest;

ImageDAO idao=new ImageDAO();

public String execute() {
    try {       
        String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
        System.out.println("Server path:" + filePath);

        File fileToCreate = new File(filePath,this.userImageFileName);

        System.out.println("fileToCreate="+fileToCreate.getName());
        FileUtils.copyFile(this.userImage, fileToCreate);

        // call function in DAO to save image in  database;;;;;;

        boolean res=idao.saveProfileImage(filePath+"\\"+fileToCreate.getName());

        //read image method call code...........
        if(res == true){

            //call read method...1) get blob from db i.e. binary stream2) then create outputstream then pass it to jsp after writing to it.
        //3) and display..
         }

        //read call end

        if(res == true){
        return SUCCESS;
        }

    } catch (Exception e) {
        e.printStackTrace();
        addActionError(e.getMessage());

        return INPUT;
    }

    return SUCCESS;
}

public File getUserImage() {
    return userImage;
}

public void setUserImage(File userImage) {
    this.userImage = userImage;
}

public String getUserImageContentType() {
    return userImageContentType;
}

public void setUserImageContentType(String userImageContentType) {
    this.userImageContentType = userImageContentType;
}

public String getUserImageFileName() {
    return userImageFileName;
}

public void setUserImageFileName(String userImageFileName) {
    this.userImageFileName = userImageFileName;
}

@Override
public void setServletRequest(HttpServletRequest servletRequest) {
    this.servletRequest = servletRequest;        
}

希望你有这个场景。

提前致谢。

0 个答案:

没有答案