如何使用 Jsp <p:fileUpload 中的条件

How use conditions in Jsp <p:fileUpload

我需要检查上传文件的大小、类型并将上传的文件名设置为 fileUpload 值标签。
我在 uploadController 中为它们编写了简单的函数,需要调用它们来检查文件大小和类型。 是否有任何推荐的方法来执行此文件验证?

<h:form enctype="multipart/form-data">
  <p:growl id="messages" showDetail="true" />


  <p:fileUpload 

        // need set value after validating the file using  
           BannerUpload.isPng()
           BannerUpload.checkMaxSize()
  
        value="#{BannerUpload.file}" mode="simple" skinSimple="true" />
  <br/>

  <ui:fragment rendered="#{not empty BannerUpload.file}">
    <img src="data:image/png;base64,#{BannerUpload.imageContentsAsBase64}" alt="" />
  </ui:fragment>
  <br/>

  <p:commandButton action="#{BannerUpload.preview}" ajax="false" value="Preview" />

  <br/>

  <p:commandButton value="Submit" ajax="false" action="#{BannerUpload.upload}" disabled="false" />
</h:form>

检查是不是图片?或者图像的大小可以在浏览器端完成这里我找到了 link click here for example 你也可以使用控制器,但它不适合大文件和示例 javascript 代码

function GetFileSize(fileid) {
try {
    var fileSize = 0;
    // for IE
    if(checkIE()) { //we could use this $.browser.msie but since it's deprecated, we'll use this function
        // before making an object of ActiveXObject, 
        // please make sure ActiveX is enabled in your IE browser
        var objFSO = new ActiveXObject("Scripting.FileSystemObject");
        var filePath = $("#" + fileid)[0].value;
        var objFile = objFSO.getFile(filePath);
        var fileSize = objFile.size; //size in b
        fileSize = fileSize / 1048576; //size in mb 
    }
    // for FF, Safari, Opeara and Others
    else {
        fileSize = $("#" + fileid)[0].files[0].size //size in b
        fileSize = fileSize / 1048576; //size in mb 
    }
    alert("Uploaded File Size is" + fileSize + "MB");
}
catch (e) {
    alert("Error is :" + e);
}

}