--9.18
This commit is contained in:
47
src/main/java/com/peanut/common/utils/FileUtils.java
Normal file
47
src/main/java/com/peanut/common/utils/FileUtils.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package com.peanut.common.utils;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 8.2上传测试工具类
|
||||
*/
|
||||
public class FileUtils {
|
||||
|
||||
/**
|
||||
* @param file 文件
|
||||
* @param path 文件存放路径
|
||||
* @param fileName 保存的文件名
|
||||
* @return
|
||||
*/
|
||||
public static boolean upload(MultipartFile file, String path, String fileName) {
|
||||
|
||||
//确定上传的文件名
|
||||
String realPath = path + "\\" + fileName;
|
||||
System.out.println("上传文件:" + realPath);
|
||||
|
||||
File dest = new File(realPath);
|
||||
|
||||
//判断文件父目录是否存在
|
||||
if (!dest.getParentFile().exists()) {
|
||||
dest.getParentFile().mkdir();
|
||||
}
|
||||
|
||||
try {
|
||||
//保存文件
|
||||
file.transferTo(dest);
|
||||
return true;
|
||||
} catch (IllegalStateException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user