Sfoglia il codice sorgente

照片上传接口调整

dzx 1 anno fa
parent
commit
9ebafc53cd

+ 15 - 1
src/main/java/com/xjrsoft/module/student/service/impl/StudentManagerServiceImpl.java

@@ -65,6 +65,7 @@ import com.xjrsoft.module.system.mapper.DictionarydetailMapper;
 import com.xjrsoft.module.system.mapper.DictionaryitemMapper;
 import com.xjrsoft.module.system.service.IAreaService;
 import lombok.AllArgsConstructor;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -962,8 +963,21 @@ public class StudentManagerServiceImpl extends MPJBaseServiceImpl<BaseStudentUse
     @Override
     public Boolean uploadImage(Long userId, MultipartFile file) throws IOException {
         BaseStudentUser studentUser = studentbaseManagerBaseStudentUserMapper.selectById(userId);
+        String[] imgSuffix = new String[]{"png", "jpg", "jpeg"};
+        String suffix = StringUtils.substringAfterLast(file.getName(), StringPool.DOT);
+        if (!Arrays.asList(imgSuffix).contains(suffix)) {
+            throw new MyException("图片格式不正确!");
+        }
         String base64String = Base64.getEncoder().encodeToString(file.getBytes());
-        studentUser.setAvatar(base64String);
+
+        Map<String, String> map = new HashMap<String, String>() {
+            {
+                put("jpg", "data:image/jpg;base64,");
+                put("jpeg", "data:image/jpeg;base64,");
+                put("png", "data:image/png;base64,");
+            }
+        };
+        studentUser.setAvatar(map.get(suffix) + base64String);
         studentbaseManagerBaseStudentUserMapper.updateById(studentUser);
         return true;
     }