首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用ClamAV检查上载文件中的病毒

使用ClamAV检查上载文件中的病毒
EN

Code Review用户
提问于 2018-04-10 10:30:48
回答 1查看 3.6K关注 0票数 1

我必须检查上传的防毒文件,如果有异常,我必须根据异常给用户一个消息。如果这是一个超时,我会要求用户尝试一下。

我的第一堂课,fileUpload是:

代码语言:javascript
运行
复制
public class FileUpload {

    //Message when no virus found.     
    private static final String NO_VIRUS_FOUND =  "No virus found";


    //More attributes and functions


    public void checkFile(FacesContext ctx, UIComponent comp, Object value) throws IOException {
          file = (Part) value;
          if (file != null) {
                  String antiVirusResult;    
                  antiVirusResult = FileCheckClass.checkVirus(getConfig(), file.getInputStream(), NO_VIRUS_FOUND);
                 if (!antiVirusResult.contains(NO_VIRUS_FOUND)) {

                     generateAntiVirusMessages(antiVirusResult);
                     throw new ValidatorException(msgs);
                 }
          }
     }

}

我的seond类,FileCheck类有一个checkVirus()函数:

代码语言:javascript
运行
复制
public static String checkVirus(FileConfig fileconfig, InputStream inputStream, String noVirusFound) {
        try {

            ClamAVClient cl = new ClamAVClient(fileConfig.getClamavHost(), fileConfig.getClamavPort(), DEFAULT_TIMEOUT);      

            if(ClamAVClient.isCleanReply(cl.scan(inputStream))){
                return noVirusFound;
            }
        } catch (Exception e) {
            logger.error("Error while scanning file: ", e);
            return e.toString();
        }
        return noVirusFound;
    }

从check函数中,我必须返回一个字符串,以便从第一个类将NO_VIRUS_FOUND传递给它。

  • 将字符串从一个类传递到另一个类是一种可接受的做法,只是为了在末尾进行字符串比较。我担心,如果我在两个类中分别定义字符串,那么一个类中的更改将严重影响另一个类中函数的输出。
  • 对上述情况,是否可以提出一些改善意见呢?
EN

回答 1

Code Review用户

回答已采纳

发布于 2018-04-10 11:33:46

  1. 似乎存在逻辑上的错误:如果ClamAVClient.isCleanReply()返回false,但没有抛出异常,该怎么办?这里没有其他子句,而且checkVirus将不会返回找到的病毒。
  2. 与其返回String,为什么checkVirus()不重新抛出异常?这样,它还可以返回病毒名称的字符串(或从ClamAVClient生成的任何内容)。
  3. 与其传递字符串,不如将其公开。然后任何类都可以将其称为FileUpload.NO_VIRUS_FOUND
  4. 更好的是,用常量来做一个枚举。这样,您可以向枚举添加值(比如VIRUS_FOUND .)

虽然有争议,但异常编码被认为是一种反模式。异常应保留为错误条件,而不是作为发现病毒的指示。

编辑:

关于如何实现pt 2的问题:首先,我将创建一个custum异常,表示病毒检查失败。

代码语言:javascript
运行
复制
public class VirusCheckException extends Exception {
    public VirusCheckException(Throwable cause) {
        super(cause);
    }
    public VirusCheckException(String message, Throwable cause) {
        super(message, cause);
    }
    // also rest of constructors...
}

然后,抛出自定义异常,而不是返回异常消息。返回值被释放为从扫描返回更多丰富的数据(如果可能的话):

代码语言:javascript
运行
复制
public static String checkVirus(FileConfig fileconfig, InputStream inputStream, String noVirusFound) 
throws VirusCheckException {
    try {
        ClamAVClient cl = new ClamAVClient(fileConfig.getClamavHost(), fileConfig.getClamavPort(), DEFAULT_TIMEOUT);      

        Object response = cl.scan(inputStream);
        if (ClamAVClient.isCleanReply(response)) {
            return noVirusFound;
        } else {
            return ClamAVClient.getVirusName(response);  // assuming there is such...
        }
    } catch (Exception e) {
        return new VirusCheckException("Virus Found!", e);
    }
}

现在,您必须在callng方法中捕获该异常并处理它:

代码语言:javascript
运行
复制
public void checkFile(...) {
    try {
        antiVirusResult = FileCheckClass.checkVirus(getConfig(), file.getInputStream(), NO_VIRUS_FOUND);
    } catch (VirusCheckException e) {
        generateAntiVirusMessages(e.getMessage());
        throw new ValidatorException(msgs);
    }
}
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/191680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档