文件上传白名单控制

1、字典表中添加白名单 value文件格式

INSERT INTO `pt_dictionary` (`id`,`sign`,`code`,`name`,`value`, `sort`,`isDefault`,`state`,`active`)

 VALUES ('whiteList','whiteList',0,'上传文件格式格式白名单','.png.jpg.xsls.log',99,0,1,1);

 

 

一、前端验证方式

2、common模块添加查询白名单方法pmp.js pt.varification方法

pt.varification(fileName)方法

fileName:文件名

回参 true:格式通过

     false:格式不通过

3、js中上传前调用白名单方法,进行文件格式对比,不在白名单内提示白名单中得格式

//格式验证

if(!pt.verification(fileName)){

return;

}

//多个

for(var i=0;i<files.length;i++){

if(!pt.verification(files[i].name)){

return;

}

}

//演示js

function hosue_uploadAttachment(par) {
var fileName = $("#selectAttachment").val();
if (fileName == null) {
return;
}
var state = pt.verification(fileName);//格式验证
if(!state){
return;
}
$('#fileForm').ajaxSubmit({
url : pt.base + "sb/sbtz/uploadAttachment.do",
type : 'post',
dataType : 'json',
async : false,
success : function(result) {
pt.showMsg(result.msg);
var list = result.data;
if (list != null) {
$("#loadFileId").val(list[0]);
$("#fileDiv").val("");
$("#fileDiv").val(list[3] + "个文件");
}
}
});
}

//格式验证方法

pt.verification = function(fileName){

var state;

var format = fileName.substr(fileName.lastIndexOf(".")+1,fileName.length);
$.ajax({
async: false,
type : 'post',
url : pt.base + "pt/dict/queryWhiteList.do",
data : {
"format" : format
},
dataType : "json",
success : function(result) {
if(result.state){
state = true;
}else{
pt.showErrorMsg("请选择"+result.data+"格式文件!");
state = false;
}
}
});
return state;
}

 // 获取白名单信息并判断

@ResponseBody
@OperationLog(rank = RankLevelEnum.COMMON, module = ModuleEnum.COMMON, entity = "ptDictionary",
name = "queryWhiteList", type = OperatonTypeEnum.QUERY, title = "查询字典下拉框")
@RequestMapping(value = "queryWhiteList", method = RequestMethod.POST)
public AjaxResult queryWhiteList(HttpServletRequest request,HttpServletResponse response) {
//结果实体类
AjaxResult result = new AjaxResult();
//format文件格式 String format
= request.getParameter("format");
//查询字典表中白名单格式 PtDictionaryEntity ptDictionaryEntity
= ptDictionaryService.findById("whiteList"); String[] splits = StringUtils.split(ptDictionaryEntity.getValue(), "."); List<String> strings = Arrays.asList(splits); result.setState(strings.contains(format)); result.setData(ptDictionaryEntity.getValue()); return result; }

二、后台验证方式

1、调用附件上传格式验证公共方法

//多个文件判断List<MultipartFile>集合

PtAttachmentServiceImpl.whiteListVerifys(files)方法

files:文件集合

回参AjaxResult实体类

判断AjaxResult.getState

true:格式通过

false:格式不通过

AjaxResult.getMsg 格式不正确的文件信息

格式不通过return AjaxResult 前端做出提示pt.showMsg(result.msg);

List<MultipartFile> files = multipartRequest.getFiles("file");
AjaxResult varifysResult = ptAttachmentService.whiteListVerifys(files); if(!varifysResult.getState()) { return varifysResult; }
//单个文件判断MultipartFile

PtAttachmentServiceImpl.whiteListVerify(file)方法

file:单个文件

回参AjaxResult实体类

判断AjaxResult.getState

true:格式通过

false:格式不通过

格式不通过return AjaxResult 前端做出提示pt.showMsg(result.msg);

MultipartFile file = multipartRequest.getFiles("file");
        AjaxResult varifysResult = ptAttachmentService.whiteListVerifys(file);
        if(!varifysResult.getState()) {
            return varifysResult;
        }
  /**
   *多个文件判读方法
   *files文件列表
   *return AjaxResult结果集实体类
  */
  @Override
public AjaxResult whiteListVerifys(List<MultipartFile> files) { AjaxResult result = new AjaxResult(); PtDictionaryEntity ptDictionaryEntity = ptDictionaryService.findById("whiteList"); String[] splits = StringUtils.split(ptDictionaryEntity.getValue(), "."); List<String> strings = Arrays.asList(splits); String msg = ""; for(int i = 0; i < files.size(); i++) { String fileName = files.get(i).getOriginalFilename(); String format = fileName.substring(fileName.lastIndexOf(".") + 1); if(!strings.contains(format)) { msg += files.get(i).getOriginalFilename()+","; result.setState(false); continue; } } result.setMsg("文件"+msg+"格式不正确,请上传"+ptDictionaryEntity.getValue()+"格式的文件"); return result; }

/**
   *多个文件判读方法
   *files文件列表
   *return AjaxResult结果集实体类
  */
@Override
 public AjaxResult whiteListVerify(MultipartFile file) {
   AjaxResult result = new AjaxResult(); 
  PtDictionaryEntity ptDictionaryEntity = ptDictionaryService.findById("whiteList"); 
  String[] splits = StringUtils.split(ptDictionaryEntity.getValue(), "."); 
  List<String> strings = Arrays.asList(splits); 
  String msg = ""; String fileName = file.getOriginalFilename(); 
  String format = fileName.substring(fileName.lastIndexOf(".") + 1); 

  if(!strings.contains(format)) { msg += file.getOriginalFilename()+","; 
    result.setState(false); result.setMsg("文件"+msg+"格式不正确,请上传"+ptDictionaryEntity.getValue()+"格式的文件"); 
  } 

  return result;
 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2021-12-18 11:48  qy12345  阅读(917)  评论(0)    收藏  举报