Commit bd47a5be authored by 以墨为白's avatar 以墨为白 🎧

图片上传-bug优化

parent 04b3a344
......@@ -3,6 +3,7 @@ package com.zksy.szpt.aspect;
import com.zksy.szpt.domain.HttpResult;
import com.zksy.szpt.domain.HttpResultState;
import com.zksy.szpt.exception.NotificationException;
import org.apache.ibatis.javassist.NotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -135,11 +136,11 @@ public class GlobalExceptionHandler {
//endregion
// @ResponseBody
// @ExceptionHandler({NotificationException.class})
// public HttpResult<HttpResultState> NotificationExceptionHandler(NotificationException e) {
// HttpResultState state = HttpResultState.NOTIFICATION;
// state.setMessage(e.getMessage());
// return new HttpResult<>(state);
// }
@ResponseBody
@ExceptionHandler({NotificationException.class})
public HttpResult<HttpResultState> NotificationExceptionHandler(NotificationException e) {
HttpResultState state = HttpResultState.NOTIFICATION;
state.setMessage(e.getMessage());
return new HttpResult<>(state);
}
}
package com.zksy.szpt.controller;
import com.zksy.szpt.domain.UploadImageDTO;
import com.zksy.szpt.exception.NotificationException;
import com.zksy.szpt.service.ImageUploadService;
import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated;
......@@ -21,7 +22,7 @@ public class ImageUploadController {
}
@PostMapping("/upload")
public Integer upload(@RequestBody @Validated UploadImageDTO uploadImageDTO) {
public Integer upload(@RequestBody @Validated UploadImageDTO uploadImageDTO) throws NotificationException {
return imageUploadService.upload(uploadImageDTO);
}
}
......@@ -19,7 +19,7 @@ public class ImageUploadService {
this.xxImgfileMapper = xxImgfileMapper;
}
public Integer upload(UploadImageDTO uploadImageDTO) {
public Integer upload(UploadImageDTO uploadImageDTO) throws NotificationException {
// 创建 WebClient 实例用于下载文件
WebClient webClient = WebClient.builder()
.baseUrl(uploadImageDTO.getImagePath())
......@@ -46,9 +46,16 @@ public class ImageUploadService {
xxImgfile.setOurl(uploadImageDTO.getImagePath());
xxImgfile.setNwzh("0");
// 等待文件内容下载完成
byte[] fileContent = fileContentMono.block();
xxImgfile.setData(fileContent);
return xxImgfileMapper.insert(xxImgfile);
try {
byte[] fileContent = fileContentMono.block();
if (fileContent == null) {
throw new NotificationException("Failed to download file");
}
xxImgfile.setData(fileContent);
return xxImgfileMapper.insert(xxImgfile);
} catch (Exception e) {
throw new NotificationException(uploadImageDTO.getImagePath() + ",An error occurred while downloading the file");
}
// 将文件内容插入数据库
// fileContentMono.flatMap(fileContent -> {
// try {
......
......@@ -94,7 +94,7 @@ public class MainTest {
Assertions.assertNotNull(secretKey, "appId不存在");//断言appId存在,为空直接抛出异常不进行下一步测试,提高测试效率
//请求参数
UploadImageDTO uploadImageDTO = new UploadImageDTO();
uploadImageDTO.setImagePath("http://192.168.168.211/guoqing.jpg");
uploadImageDTO.setImagePath("http://192.168.168.211/guoqing1.jpg");
uploadImageDTO.setImageType(ListImageType.FACE);
String json = null;
try {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment