parent
a9ff7eed94
commit
966b2063e6
10 changed files with 181 additions and 63 deletions
@ -0,0 +1,70 @@ |
||||
/* |
||||
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are met: |
||||
* |
||||
* Redistributions of source code must retain the above copyright notice, |
||||
* this list of conditions and the following disclaimer. |
||||
* Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* Neither the name of the dreamlu.net developer nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* Author: Chill 庄骞 (smallchill@163.com) |
||||
*/ |
||||
package org.springblade.common.utils; |
||||
|
||||
import org.springframework.web.multipart.MultipartFile; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.time.LocalDate; |
||||
import java.util.Map; |
||||
import java.util.UUID; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
import java.util.function.Function; |
||||
import java.util.function.Predicate; |
||||
|
||||
/** |
||||
* 通用工具类 |
||||
* |
||||
* @author Chill |
||||
*/ |
||||
public class FileUploadUtil { |
||||
static final String FILE_SAVE_PATH = "/root/docker-nginx/html/mpimg"; |
||||
|
||||
/** |
||||
* 上传文件 |
||||
* |
||||
* @param file |
||||
* @return |
||||
*/ |
||||
public static String UploadFile(MultipartFile file) { |
||||
String dirPath = "/" + LocalDate.now().getYear() + "-" + LocalDate.now().getMonthValue(); |
||||
//判断该路径是否存在
|
||||
File dir = new File(FILE_SAVE_PATH + dirPath); |
||||
if (!dir.exists()) { |
||||
//如果这个文件夹不存在的话,就创建这个文件
|
||||
dir.mkdirs(); |
||||
} |
||||
|
||||
//获取文件名
|
||||
String fileName = file.getOriginalFilename(); |
||||
//获取文件后缀名
|
||||
String suffixName = fileName.substring(fileName.lastIndexOf(".")); |
||||
//重新生成文件名
|
||||
fileName = UUID.randomUUID() + suffixName; |
||||
|
||||
//完成文件上传
|
||||
try { |
||||
file.transferTo(new File(FILE_SAVE_PATH + dirPath + "/" + fileName)); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
|
||||
return dirPath + "/" + fileName; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue