图像未从目标路径显示

时间:2016-04-28 03:50:45

标签: image file-upload struts2

我正在上传jsp中的图片:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>

<body>
<h1>Struts 2 &lt;s:file&gt; file upload example</h1>

<form action="resultAction" namespace="/" method="POST" enctype="multipart/form-data">

<s:file name="imageUpload" label="Select a image to upload" size="3400" />

<s:submit value="submit" name="submit" />

</form>

</body>
</html>

我定义的struts.xml:

    <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

    <constant name="struts.devMode" value="true" />
    <constant name="struts.custom.i18n.resources" value="global" />
    <constant name="struts.multipart.saveDir" value="/home/avijeet/CampuStore/" />



    <package name="default" namespace="/" extends="struts-default">

       <action name="resultAction" class="dto.Image">

            <interceptor-ref name="exception"/>
            <interceptor-ref name="i18n"/>
            <interceptor-ref name="fileUpload">
                        <param name="allowedTypes">
                            image/png,
                            image/jpeg
                        </param>
                        <param name="maximumSize">1000000</param>
            </interceptor-ref> 
            <interceptor-ref name="params">
                <param name="excludeParams">dojo\..*,^struts\..*</param>
            </interceptor-ref>
            <interceptor-ref name="validation">
                <param name="excludeMethods">input,back,cancel,browse</param>
            </interceptor-ref>
            <interceptor-ref name="workflow">
                <param name="excludeMethods">input,back,cancel,browse</param>
            </interceptor-ref>

            <result name="success">result.jsp</result>
            <result name="input">imageupload.jsp</result>

        </action>


        <action name="fileUploadAction" class="dto.Image" method="display">
                        <result name="none">imageupload.jsp</result>
        </action>




    </package>
</struts>    

结果页面result.jsp:

        <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>

<body>
<h1>Struts 2 &lt;s:file&gt; file upload example</h1>

<h4>
   File Name : <s:property value="imageUploadFileName"/> 
</h4> 

<h4>
   Content Type : <s:property value="imageUploadContentType"/> 
</h4> 

<h4>
   File : <s:property value="imageUpload"/> 
</h4> 

   <img src="<s:property value="destPath"/>"/>



</body>
</html>

动作类:

public class Image extends ActionSupport{


    private File imageUpload;
    private String imageUploadContentType;
    private String imageUploadFileName;
    private String destPath;

    /**
     * @return the imageUpload
     */

    /**
     * @return the imageUploadContentType
     */
    public String getImageUploadContentType() {
        return imageUploadContentType;
    }

    /**
     * @param imageUploadContentType the imageUploadContentType to set
     */
    public void setImageUploadContentType(String imageUploadContentType) {
        this.imageUploadContentType = imageUploadContentType;
    }

    /**
     * @return the imageUploadFileName
     */
    public String getImageUploadFileName() {
        return imageUploadFileName;
    }

    /**
     * @param imageUploadFileName the imageUploadFileName to set
     */
    public void setImageUploadFileName(String imageUploadFileName) {
        this.imageUploadFileName = imageUploadFileName;
    }

    @Override
    public String execute() throws Exception{

        try {
            String filePath = "/media/avijeet/ENTERTAINMENT";
            File destFile = new File(filePath, imageUploadFileName);
            FileUtils.copyFile(imageUpload, destFile);
            this.setDestPath(filePath + "/" + imageUploadFileName);




        }catch(IOException e){
            System.err.println(e);
            return ERROR;
        }
        return SUCCESS;

        }

        public String display() {
                return NONE;
        }

    /**
     * @return the fileUpload
     */


    /**
     * @return the imageUpload
     */
    public File getImageUpload() {
        return imageUpload;
    }

    /**
     * @param imageUpload the imageUpload to set
     */
    public void setImageUpload(File imageUpload) {
        this.imageUpload = imageUpload;
    }

    /**
     * @return the destPath
     */
    public String getDestPath() {
        return destPath;
    }

    /**
     * @param destPath the destPath to set
     */
    public void setDestPath(String destPath) {
        this.destPath = destPath;
    }




}

但结果是没有图像。网页无法加载图像,而GlassFish从不识别目录。结果是: The image where it is supposed to get shown appears broken

0 个答案:

没有答案