项目归纳总结
近期做的项目任务中学习了一些新的东西
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
注意事项
其中必要的东西有
- el-form-item中的prop
- el-form-item中的:rules
可以选的东西有
-
:rules中的required(是不是一定要填)
-
:rules中的message(如果不符合,显示的东西)
-
:rules中的type(是不是规定的类型,值得注意的是如果规定的是number,须给input搭配上v-model.number )
-
:rules中的trigger(什么事件会触发)
-
方法中的触发检查
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>