private static final String IMG_PATH = "D:\\upload\\resources\\images\\";
private static final String DOC_PATH = "D:\\upload\\resources\\doc\\";
public static String upload(MultipartFile file) throws Exception {
String filename = file.getOriginalFilename();
String suffix = filename.substring(filename.lastIndexOf(".")).toLowerCase();
String partPath = "";
String pathName;
if (img_check(suffix)) {
if (file.getSize() > 2 * 1024 * 1024) {
throw new IllegalArgumentException("上传文件大小不能超过2M");
}
partPath = IMG_PATH;
pathName = "images";
} else if (doc_check(suffix)) {
if (file.getSize() > 10 * 1024 * 1024) {
throw new IllegalArgumentException("上传文件大小不能超过10M");
}
partPath = DOC_PATH;
pathName = "doc";
} else {
throw new IllegalArgumentException("不支持的文件格式");
}
return uploadResources(file, partPath, pathName);
}
public static String uploadResources(MultipartFile file, String path, String pathName) {
File folder = new File(path);
if (!folder.exists()) {
folder.mkdirs();
}
String fileName = file.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
img_check(suffix);
String uuidName = uuidName(fileName);
try {
file.transferTo(new File(path + uuidName));
return "http://" + InetAddress.getLocalHost().getHostAddress() + "/resources/" + pathName + "/" + uuidName;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String uploadLocalResources(String filePathName) {
try {
String suffix = filePathName.substring(filePathName.lastIndexOf(".")).toLowerCase();
String filePath = "";
String fileType;
if (img_check(suffix)) {
filePath = IMG_PATH;
fileType = "images";
} else if (doc_check(suffix)) {
filePath = DOC_PATH;
fileType = "doc";
} else {
return null;
}
File file = new File(filePath);
if (!file.isDirectory()) {
file.mkdirs();
}
String uuidFileName = uuidName(FilenameUtils.getName(filePathName));
String makeFilePath = filePath + uuidFileName;
try {
File targetFile = new File(makeFilePath);
File localFile = new File(filePathName);
InputStream stream = new FileInputStream(localFile);
byte[] buffer = new byte[1024 * 1024];
FileOutputStream fos = new FileOutputStream(targetFile);
int byteRead = 0;
while ((byteRead = stream.read(buffer)) != -1) {
fos.write(buffer, 0, byteRead);
fos.flush();
}
fos.close();
stream.close();
return "http://" + InetAddress.getLocalHost().getHostAddress() + "/resources/" + fileType + "/" + uuidFileName;
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String uuidName(String filename) {
filename = UUID.randomUUID().toString().replace("-", "") + filename.substring(filename.lastIndexOf("."));
return filename;
}
private static boolean img_check(String targetValue) {
String[] arr = new String[]{".jpg", ".jpeg", ".png", ".gif"};
return Arrays.asList(arr).contains(targetValue);
}
public static boolean doc_check(String targetValue) {
String[] arr = new String[]{".doc", ".pdf", ".docx"};
return Arrays.asList(arr).contains(targetValue);
}