上传图片工具类
组件用的easyui,可以替换自己喜欢的组件
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
/**
* 上传通用类
* */
public class DataUploadUtils {
/**
* 默认大小 10M //字节
*/
public static final long DEFAULT_MAX_SIZE = 10485760;
/**
* 默认的文件名最大长度
*/
public static final int DEFAULT_FILE_NAME_LENGTH = 100;
public static String upFile(MultipartFile file, String uploadPath) throws IOException, FileSizeException, FileNameLengthException
{
//判断文件大小
// assertAllowed(file);
String fileName = file.getOriginalFilename();//获取文件名加后缀 xxx.jpg
if(fileName.contains(File.separator)){
//string.contains("xxx");用于判断字符串中是否含有子字符串XXX
fileName = fileName.substring(fileName.lastIndexOf(File.separator)+1);
}
// File file1 = new File(uploadPath+ File.separator);
File f = new File(new File(uploadPath).getAbsolutePath()+ "\\");
// 判断文件父目录是否存在
if (!f.exists()) {
f.mkdirs();
}
try {
File destFile = new File(f, fileName); // ---src/erp_web/upload/images/logos/xxx.jpg
file.transferTo(destFile);
}catch (IOException e) {
return "上传失败,请通知管理员!";
}
return uploadPath+"/"+fileName;
}
public static boolean upFile2(String fileName, String uploadPath) throws IOException, FileSizeException, FileNameLengthException
{
//判断文件大小
// assertAllowed(file);
if(fileName.contains(File.separator)){
fileName = fileName