2

我可以使用struts2上传文件。但我想在 struts 2 中显示有关上传文件的详细信息 ( Filename, filesize, )。filetype

我正在尝试使用<s:property value="myFile" />标签获取详细信息,但始终显示 [Object HTMLDocument]。

我的代码如下。

uploadfile.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
                pageEncoding="ISO-8859-1"%>
            <%@ taglib prefix="s" uri="/struts-tags"%>
            <!DOCTYPE html>

            <html>
            <head>
                <meta charset="utf-8">

            </head>
            <body>
                <div id="success-image">
                 <s:property value="myFile" />
            <s:head theme="ajax" />
                <s:form id="asset-upload"  name="asset-upload" action="uploadFile.action" method="POST"  theme="ajax" enctype="multipart/form-data">
                <s:file name="myFile" label="Upload Image"/>
       <s:submit targets="success-image" showLoadingText="false" indicator="uploading-image" id="asdf"/>
    </s:form>
        </div>
            </body>
            </html>

struts.xml

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

    <struts>
        <constant name="struts.enable.DynamicMethodInvocation" value="false" />
        <constant name="struts.devMode" value="false" />
        <constant name="struts.multipart.maxSize" value="1000000000" />

        <package name="default" extends="struts-default,json-default" namespace="/">
            <action name="uploadFile" class="com.isam.struts.uploadFile">
                <result name="success">
                    <param name="myFile">myFile</param>
                 </result>
                <result name="error">login.jsp</result>
            </action>

            <action name="autoCompleterList" class="com.isam.struts.AutoCompleterList">
                <result type="json" />
            </action>
        </package>
    </struts>

UploadFile.java

        package com.isam.struts;

        import java.io.File;
        import java.io.IOException;

        import org.apache.commons.io.FileUtils;

        import com.opensymphony.xwork2.ActionSupport;

        public class uploadFile extends ActionSupport {

            private File myFile;
            private String myFileContentType;
            private String myFileFileName;
            private String destPath;


            public String execute() {
                /* Copy file to a safe location */
                destPath = "C:/temp/";

                try {
                    System.out.println("Src File name: " + myFile);
                    System.out.println("Dst File name: " + myFileFileName);

                    File destFile = new File(destPath, myFileFileName);
                    FileUtils.copyFile(myFile, destFile);

                } catch (IOException e) {
                    e.printStackTrace();
                    return ERROR;
                }

                return SUCCESS;
            }

            public File getMyFile() {
                return myFile;
            }

            public void setMyFile(File myFile) {
                this.myFile = myFile;
            }

            public String getMyFileContentType() {
                return myFileContentType;
            }

            public void setMyFileContentType(String myFileContentType) {
                this.myFileContentType = myFileContentType;
            }

            public String getMyFileFileName() {
                return myFileFileName;
            }

            public void setMyFileFileName(String myFileFileName) {
                this.myFileFileName = myFileFileName;
            }

        }

所以我应该在jsp文件中添加什么来显示细节,以及我应该在struts.xml文件中添加什么

4

2 回答 2

1

uploadFile行动中,您需要为 info 和 getter 放置 setter 以在结果中显示它。

public void setUpload(File myFile) {
  this.myFile= myFile;
}

public void setUploadContentType(String myFileContentType) {
  this.myFileContentType= myFileContentType;
}

public void setUploadFileName(String myFileFileName) {
  this.myFileFileName = myFileFileName;
}


public String getMyFileFileName() {
  return myFileFileName;
}

public String getMyFileSize() {
  return myFile.length();
}

public String getMyFileContentType() {
  return myFileContentType;
}

它以注入可以进一步显示的值命名

<s:property value="myFileName" />

<s:property value="myFileSize" />

<s:property value="myFileContentType" />

如果文件有扩展名,您可以从 myFile. 如果它具有图像类型,那么您应该应用编解码器来获取它和尺寸。但是,它不是必需的。获得后,BufferedImage您可以写入或缩放到任何图像类型或大小。

错误的是结果“成功”。结果应该是带有 id image 参数的重定向操作的正常 URL,如果你想取回它,你必须再次加载它。

于 2013-02-15T10:23:46.523 回答
1

要在上传后在同一页面上显示文件详细信息,请将您的操作结果更改为:

<action name="uploadFile" class="com.isam.struts.uploadFile">
   <result name="success">uploadfile.jsp</result>
   <result name="error">login.jsp</result>
</action>

而在 JSP 中使用<s:property>标签来显示值:

<s:property value="myFile.length()"/>
<s:property value="myFileContentType"/>
<s:property value="myFileFileName"/>
于 2013-02-15T10:58:54.253 回答