项目归纳总结 MultipartFile文件保存 文件解压 element-UI的rules

12 篇文章 0 订阅
8 篇文章 0 订阅

项目归纳总结

近期做的项目任务中学习了一些新的东西

MultipartFile文件保存

解析都在注释里了

public static boolean saveMultipartFile(MultipartFile file,String path) {
    // 如果文件为空 则返回
    if (file.isEmpty()) {
        return false;
    }
    // 获取文件名
    String fileName = file.getOriginalFilename();
    // 新建一个文件对象
    File dest = new File(new File(path).getAbsolutePath()+ "/" + fileName);
    // 如果目的地址没有创建,那么就创建
    if (!dest.getParentFile().exists()) {
        dest.getParentFile().mkdirs();
    }
    try {
        // 保存文件
        file.transferTo(dest);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

文件解压

public static boolean unZip(String path){
    int count = -1;
    String savepath = "";
    File file = null;
    InputStream is = null;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    //保存解压文件目录
    savepath = path.substring(0, path.lastIndexOf(".")) + File.separator;
    //创建保存目录
    new File(savepath).mkdir();
    ZipFile zipFile = null;
    try
    {
        //解决中文乱码问题
        zipFile = new ZipFile(path, Charset.forName("gbk"));
        Enumeration<?> entries = zipFile.entries();
        while(entries.hasMoreElements())
        {
            byte buf[] = new byte[BUFFER];
            ZipEntry entry = (ZipEntry)entries.nextElement();
            String filename = entry.getName();
            boolean ismkdir = false;
            //检查此文件是否带有文件夹
            if(filename.lastIndexOf("/") != -1){
                ismkdir = true;
            }
            filename = savepath + filename;
            //如果是文件夹先创建
            if(entry.isDirectory()){
                file = new File(filename);
                file.mkdirs();
                continue;
            }
            file = new File(filename);
            //如果是目录先创建
            if(!file.exists()){
                if(ismkdir){
                    //目录先创建
                    new File(filename.substring(0, filename.lastIndexOf("/"))).mkdirs();
                }
            }
            //创建文件
            file.createNewFile();
            is = zipFile.getInputStream(entry);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos, BUFFER);
            while((count = is.read(buf)) > -1)
            {
                bos.write(buf, 0, count);
            }
            bos.flush();
            bos.close();
            fos.close();
            is.close();
        }
        zipFile.close();
        return true;
    }catch(IOException ioe){
        ioe.printStackTrace();
        return false;
    }finally{
        try{
            if(bos != null){
                bos.close();
            }
            if(fos != null) {
                fos.close();
            }
            if(is != null){
                is.close();
            }
            if(zipFile != null){
                zipFile.close();
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}

element-UI的rules

引用element-UI官方的 https://element.eleme.cn/#/zh-CN/component/form#form-attributes

注意事项

其中必要的东西有

  1. el-form-item中的prop
  2. el-form-item中的:rules

可以选的东西有

  1. :rules中的required(是不是一定要填)

  2. :rules中的message(如果不符合,显示的东西)

  3. :rules中的type(是不是规定的类型,值得注意的是如果规定的是number,须给input搭配上v-model.number )

  4. :rules中的trigger(什么事件会触发)

  5. 方法中的触发检查

    this.$refs[formName].validate((valid) => {
        if (valid) {
           	//检查成功了
        } else {
            //检查失败了
        }
    });
    

例子

<el-form :model="numberValidateForm" ref="numberValidateForm" label-width="100px" class="demo-ruleForm">
  <el-form-item
    label="年龄"
    prop="age"
    :rules="[
      { required: true, message: '年龄不能为空'},
      { type: 'number', message: '年龄必须为数字值'}
    ]"
  >
    <el-input type="age" v-model.number="numberValidateForm.age" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('numberValidateForm')">提交</el-button>
    <el-button @click="resetForm('numberValidateForm')">重置</el-button>
  </el-form-item>
</el-form>
<script>
  export default {
    data() {
      return {
        numberValidateForm: {
          age: ''
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值