diff --git a/src/main/java/com/rabbiter/em/constants/Constants.java b/src/main/java/com/rabbiter/em/constants/Constants.java index b50423c..695753e 100644 --- a/src/main/java/com/rabbiter/em/constants/Constants.java +++ b/src/main/java/com/rabbiter/em/constants/Constants.java @@ -1,7 +1,10 @@ package com.rabbiter.em.constants; import com.rabbiter.em.utils.PathUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +@Component public class Constants { public static final String CODE_200 = "200";//成功 public static final String CODE_500 = "500";//系统错误 @@ -9,8 +12,12 @@ public class Constants { public static final String CODE_401 = "401";//无权限 public static final String TOKEN_ERROR = "401";//token无效 public static final String CODE_403 = "403";//拒绝执行 + public static final String CODE_400 = "400";//无效请求 + //文件存储位置 - public static final String fileFolderPath = PathUtils.getClassLoadRootPath() + "/file/"; - public static final String avatarFolderPath = PathUtils.getClassLoadRootPath() + "/avatar/"; + @Value("${file.file}") + public String fileFolderPath ; + @Value("${file.avatar}") + public String avatarFolderPath ; } diff --git a/src/main/java/com/rabbiter/em/controller/AddressController.java b/src/main/java/com/rabbiter/em/controller/AddressController.java index 55c689d..0f55ed0 100644 --- a/src/main/java/com/rabbiter/em/controller/AddressController.java +++ b/src/main/java/com/rabbiter/em/controller/AddressController.java @@ -46,6 +46,8 @@ public class AddressController { String apiUrl = "https://api.deepseek.com/qq/" + qq; String response = restTemplate.getForObject(apiUrl, String.class); + + return Result.success(response); } catch (Exception e) { return Result.error(Constants.CODE_500, "调用DeepSeek失败: " + e.getMessage()); @@ -56,14 +58,26 @@ public class AddressController { 保存 */ @PostMapping - public Result save(@RequestBody Address address) { - boolean b = addressService.saveOrUpdate(address); - if(b){ - return Result.success(); - }else{ - return Result.error(Constants.CODE_500,"保存地址失败"); + public Result save(@RequestBody List
addresses) { + // 验证每个地址的IP格式 + String ipPattern = "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"; + for(Address address : addresses) { + String ipAddress = address.getLinkAddress(); + if(ipAddress != null && !ipAddress.matches(ipPattern)) { + return Result.error(Constants.CODE_400, "无效的IP地址格式: " + ipAddress); + } } + boolean success = true; + if(!addressService.saveOrUpdateBatch(addresses)) { + success = false; + } + + if(success){ + return Result.success(); + }else{ + return Result.error(Constants.CODE_500,"批量保存地址失败"); + } } @PutMapping diff --git a/src/main/java/com/rabbiter/em/controller/OrderController.java b/src/main/java/com/rabbiter/em/controller/OrderController.java index d510a67..f61476d 100644 --- a/src/main/java/com/rabbiter/em/controller/OrderController.java +++ b/src/main/java/com/rabbiter/em/controller/OrderController.java @@ -9,7 +9,7 @@ import com.rabbiter.em.entity.Order; import com.rabbiter.em.service.OrderService; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import com.sun.xml.internal.fastinfoset.stax.events.Util; +import com.sun.xml.fastinfoset.stax.events.Util; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; diff --git a/src/main/java/com/rabbiter/em/controller/UserController.java b/src/main/java/com/rabbiter/em/controller/UserController.java index c1f8fcc..6ded9b4 100644 --- a/src/main/java/com/rabbiter/em/controller/UserController.java +++ b/src/main/java/com/rabbiter/em/controller/UserController.java @@ -13,7 +13,7 @@ import com.rabbiter.em.entity.User; import com.rabbiter.em.entity.dto.UserDTO; import com.rabbiter.em.service.UserService; import com.rabbiter.em.utils.TokenUtils; -import com.sun.xml.internal.fastinfoset.stax.events.Util; +import com.sun.xml.fastinfoset.stax.events.Util; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; diff --git a/src/main/java/com/rabbiter/em/service/AvatarService.java b/src/main/java/com/rabbiter/em/service/AvatarService.java index d55eee9..25c3c16 100644 --- a/src/main/java/com/rabbiter/em/service/AvatarService.java +++ b/src/main/java/com/rabbiter/em/service/AvatarService.java @@ -25,6 +25,9 @@ public class AvatarService { @Resource private AvatarMapper avatarMapper; + @Resource + private Constants constants; + public String upload(MultipartFile uploadFile){ String url = null; //通过md5判断文件是否已经存在,防止在服务器存储相同文件 @@ -42,7 +45,7 @@ public class AvatarService { System.out.println(originalFilename+" "+type); long size = uploadFile.getSize() / 1024; //文件大小,单位kb //文件不存在,则保存文件 - File folder = new File(Constants.avatarFolderPath); + File folder = new File(constants.avatarFolderPath); if(!folder.exists()){ folder.mkdir(); } @@ -69,7 +72,7 @@ public class AvatarService { } //根据文件名下载文件 public void download(String fileName, HttpServletResponse response){ - File file = new File(Constants.avatarFolderPath+fileName); + File file = new File(constants.avatarFolderPath+fileName); if(!file.exists()){ throw new ServiceException(Constants.CODE_500,"文件不存在"); } @@ -92,7 +95,7 @@ public class AvatarService { if(delete==1){ String fileName = StrUtil.subAfter(avatar.getUrl(),"/",true); System.out.println(fileName); - File file = new File(Constants.avatarFolderPath+fileName); + File file = new File(constants.avatarFolderPath+fileName); System.out.println(file.getAbsolutePath()); if(file.exists()){ diff --git a/src/main/java/com/rabbiter/em/service/FileService.java b/src/main/java/com/rabbiter/em/service/FileService.java index 5cffb7c..1832dd5 100644 --- a/src/main/java/com/rabbiter/em/service/FileService.java +++ b/src/main/java/com/rabbiter/em/service/FileService.java @@ -30,6 +30,9 @@ public class FileService extends ServiceImpl { @Resource private FileMapper fileMapper; + @Resource + private Constants constants; + public String upload(MultipartFile uploadFile){ String originalFilename = uploadFile.getOriginalFilename(); //文件原始名字 String type = originalFilename.substring(originalFilename.lastIndexOf(".")+1); //文件后缀 @@ -57,7 +60,7 @@ public class FileService extends ServiceImpl { myFile.setUrl(url); }else{ //文件不存在,则保存文件 - File folder = new File(Constants.fileFolderPath); + File folder = new File(constants.fileFolderPath); if(!folder.exists()){ folder.mkdir(); } @@ -84,9 +87,9 @@ public class FileService extends ServiceImpl { //根据文件名下载文件 public void download(String fileName, HttpServletResponse response){ - File file = new File(Constants.fileFolderPath+fileName); + File file = new File(constants.fileFolderPath+fileName); if(!file.exists()){ - throw new ServiceException(Constants.CODE_500,"文件不存在"); + throw new ServiceException(constants.CODE_500,"文件不存在"); } try { ServletOutputStream os = response.getOutputStream(); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 722ad3a..9510fdb 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -11,8 +11,9 @@ spring: max-file-size: 30MB redis: database: 0 - host: 127.0.0.1 + host: 139.196.201.231 port: 6379 + password: Amk812s$ lettuce: pool: min-idle: 0 @@ -25,3 +26,7 @@ mybatis: mapper-locations: classpath:mapper/*.xml configuration: map-underscore-to-camel-case: true + +file: + file: /home/fzhang5/file/file/ + avatar: /home/fzhang5/file/avatar/