|
|
@@ -5,15 +5,19 @@ import com.fasterxml.jackson.databind.JavaType;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
import static com.roma.romaapi.utils.JWTUtil.SIGN;
|
|
|
|
|
|
@@ -23,6 +27,8 @@ public class CommonUtil {
|
|
|
|
|
|
@Autowired
|
|
|
StringRedisTemplate stringRedisTemplate;
|
|
|
+ @Value("${upload.path}")
|
|
|
+ private String configUploadPath;
|
|
|
|
|
|
// 通过header中token信息,获取用户Id
|
|
|
public String getUserIdByHeaderAuthorization(String headerAuthorization){
|
|
|
@@ -139,4 +145,61 @@ public class CommonUtil {
|
|
|
|
|
|
return limitAfter;
|
|
|
}
|
|
|
+
|
|
|
+ public String getUUID(){
|
|
|
+ return UUID.randomUUID().toString().replace("-", "");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件后缀
|
|
|
+ * @param fileName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getSuffix(String fileName){
|
|
|
+ return fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成新的文件名
|
|
|
+ * @param fileOriginName 源文件名
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getFileName(String fileOriginName){
|
|
|
+ return this.getUUID() + this.getSuffix(fileOriginName);
|
|
|
+ }
|
|
|
+
|
|
|
+ //头像上传
|
|
|
+ public Map uploadFile(MultipartFile file) {
|
|
|
+ String value = "";
|
|
|
+ Map map = new HashMap<>();
|
|
|
+ if (file.isEmpty()) {
|
|
|
+ map.put("sysErrorCode", "59000");
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ //1.定义上传的文件
|
|
|
+ String localPath = configUploadPath;
|
|
|
+ //2.获得文件名字
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ //3.上传
|
|
|
+ //3.1 生成新的文件名
|
|
|
+ String realPath = localPath + "/" + this.getFileName(fileName);
|
|
|
+ //3.2 保存文件
|
|
|
+ File dest = new File(realPath);
|
|
|
+ //判断文件目目录是否存在,不存在则新建
|
|
|
+ if (!dest.getParentFile().exists()){
|
|
|
+ dest.getParentFile().mkdir();
|
|
|
+ }
|
|
|
+ file.transferTo(dest);
|
|
|
+ value = realPath;
|
|
|
+ map.put("uploadPath", value);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ map.put("sysErrorCode", "59000");
|
|
|
+
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ return map;
|
|
|
+ }
|
|
|
}
|