服务器端验证文件格式

package com.li.web.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class InvalidFileType {
 private static Properties properties = new Properties();
 

 static {
  try {
   properties.load(Url.class.getClassLoader().getResourceAsStream(
     "allowuploadfiletype.properties"));
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
/**
 * 判断是否允许上传的文件类型
 * @param file 上传的文件
 * @param fileContentType 该文件类型
 * @return   是否允许上传
 */
 public static boolean invalidUploadFile(File file, String contentType) {
  if (file != null && file.length() > 0) {
           String ext=contentType.substring(contentType.lastIndexOf("/")+1).toLowerCase();
   List<String> allowType = new ArrayList<String>();
   for (Object key : properties.keySet()) {
    String value = (String) properties.get(key);
    String[] values = value.split(",");
    for (String v : values) {
     allowType.add(v.trim());
    }
   }
    
   return allowType.contains(contentType)&&properties.keySet().contains(ext);

  }
  return true;

 }

 

根目录下属性文件为

gif=image/gif
jpg=image/jpg,image/jpeg,image/pjpeg
bmp=image/bmp
png=image/png
swf=application/x-shockwave-flash
doc=application/msword
txt=text/plain
xls=application/vnd.ms-excel
ppt=application/vnd.ms-powerpoint
pdf=application/pdf
exe=application/octet-stream

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值