【问题标题】:Content type not working in struts内容类型在struts中不起作用
【发布时间】:2012-05-01 20:23:36
【问题描述】:

这是写在我的验证方法中的。检查大小和空上传是否有效,但内容类型无效,我是否遗漏了什么?

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    ActionErrors errors = new ActionErrors();

    if(file1.getFileSize()==0)
    {
    errors.add("file1", new ActionMessage("error.file.required"));
    }
    else if(!file1.getContentType().equals("audio/mpeg"));
    {
    errors.add("file1",new ActionMessage("error.file.type"));
    }
    if(file1.getFileSize()>51200)
    {
    errors.add("file1",new ActionMessage("error.file.size"));
    }

    return errors;

【问题讨论】:

    标签: struts struts-1 struts-validation struts1


    【解决方案1】:

    我认为您的 else if 条件语句由于“;”而丢失签名如下:

    else if(!file1.getContentType().equals("audio/mpeg"));
    

    应该是这样的:

    else if(!file1.getContentType().equals("audio/mpeg"))
    

    【讨论】:

    • ';'只是这里发生的一个愚蠢的错误,但“音频/mpeg”不接受 mp3 文件。我发现它正在提供我的答案:)
    【解决方案2】:

    完成:

    else if(!file1.getContentType().equals("audio/mp3")) { ---- }
    

    我检查了由String ctype = file1.getContentType();上传的文件的类型,没有进行任何验证(即上传任何文件)并将其打印在jsp页面上。从那里我知道它的音频/mp3。现在所有验证都在工作。 /

    【讨论】:

    • 我只在 Chrome 上尝试过,所以内容类型(音频/mpeg)可能适用于其他浏览器
    猜你喜欢
    • 1970-01-01
    • 2016-05-17
    • 2016-11-24
    • 2017-09-17
    • 2015-12-22
    • 2020-10-28
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多