Struts 中的文件上传验证

file upload validation in Struts

这是我需要验证的上传页面。目前验证不是happening.I 认为在Struts 2 表单验证是不同的。我可以按照这个 link http://www.mkyong.com/struts/struts-validator-framework-example/ 并以类似的方式实现我的表单吗?

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/displaytag-12.tld" prefix="display"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%-- <%@ taglib prefix="s" uri="/struts-tags"%> --%>
<%@ page errorPage="error.jsp"%>

</style>
<script>
    var _validFileExtensions = [".xlsx", ".xls", ".xlt"]; 
    function Validate(oForm) {
        alert("alert message in upload form");
    if( document.getElementById("filename").files.length == 0 ){
        alert("inside if");
         document.getElementById("fileSelect").style.display=""; 
        return false;
    }
    else{
        alert("inside else");
        document.getElementById("fileSelect").style.display="none"; 

    }
    var arrInputs = oForm.getElementsByTagName("input");
    for (var i = 0; i < arrInputs.length; i++) {
        var oInput = arrInputs[i];
        if (oInput.type == "file") {
            var sFileName = oInput.value;
            if (sFileName.length > 0) {
                var blnValid = false;
                for (var j = 0; j < _validFileExtensions.length; j++) {
                    var sCurExtension = _validFileExtensions[j];
                    if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
                        blnValid = true;
                        break;
                    }
                }

                if (!blnValid) {
                    alert("not valid");
                    document.getElementById("fileFormat").style.display="";
                    return false;
                }
            }
        }
    }

    return true;
}  



</script>
<center> 
    <html:form action="/process.do?method=upload" method="POST" enctype="multipart/form-data" onsubmit="return Validate(this);">
        <table cellpadding='4' border='0' width="80%">
                <tr>
                    <td width="1%"></td>
                    <td colspan='4' align='center'>
                        <b>Upload VIP Orders Shipment Dates:</b>
                        <div style="margin: 20px 20px 20px 250px">


                         <strong><label>File :</label></strong>&nbsp; &nbsp; &nbsp; <html:file id="filename" property="uploadedFile"/><br/>
                        <input type="submit" value="Upload File" style="margin: 40px 0 0 160px"/><br /><br />
                        <div id="fileSelect" style="color:Red;display:none">No file selected for uploading.</div>
                        <div id="fileFormat" style="color:Red;display:none">Invalid File Format. Only .xlsx, .xls, and .xlt file format allowed.</div>

                        </div>
                    </td>
                </tr>
        </table>
    </html:form> 

我只想获取文件名。如果文件名为空则显示错误消息。

我希望在客户端本身进行验证,而不是将请求发送到服务器端进行验证。这是我要显示的两条消息: 没有文件 select 用于上传 ----- 如果没有 select 文件,请单击提交按钮。和 文件格式无效。仅允许 .xlsx、.xls 和 .xlt 文件格式--- 当文件不是 excel 格式时

我找到方法了:

jsp 页数:

function performAction(obj){        
    var aForm = document.forms[0];  
    var formAction = aForm.action;
    formAction = formAction.substring(0,formAction.indexOf('?'));
    if(obj == 'LOADFILE'){          
    aForm.action = formAction+'?method=loadFile';
}

            if(obj == 'UPLOADFILE'){            
            aForm.action = formAction+'?method=upload';
            }

          aForm.submit();

}

</script>

<html:form action="/process.do?method=upload" method="post" enctype="multipart/form-data">
     <table>
     <tr>                                                               
        <b> Please download the template file by 
        <A href="#" onclick="return performAction('LOADFILE')"  >clicking here</A> 
        </b>

        <strong><label>File :</label></strong><html:file property="uploadedFile" /><br/>

        <INPUT TYPE="submit" value="Upload"  onclick="return performAction('UPLOADFILE')">

        </tr>
     </table>
</html:form> 

豆子class:

public class process  extends ActionForm {

    public ActionErrors validate(ActionMapping mapping,
                HttpServletRequest request) {

            ActionErrors errors = null;

            if ("upload".equals(request.getParameter(mapping.getParameter()))) {
                errors = new ActionErrors();
                if (uploadedFile == null || uploadedFile.getFileSize()==0 ) {
                    errors.add(" ", new ActionMessage("error.file.notselected"));

                }
                else{
                    if (!uploadedFile.getFileName().contains(".xls")
                            && !uploadedFile.getFileName().contains(".xlsx")) {
                        errors.add(" ", new ActionMessage("error.file.notxlsx"));
                    }
                }

            }

            return errors;
        }
}