Commit 6a416f8a authored by 以墨为白's avatar 以墨为白 🎧

图片上传-bug优化

parent f4dd47a7
......@@ -4,17 +4,32 @@ import com.zksy.szpt.domain.UploadImageDTO;
import com.zksy.szpt.domain.po.XxImgfile;
import com.zksy.szpt.exception.NotificationException;
import com.zksy.szpt.mapper.XxImgfileMapper;
import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;
import reactor.netty.tcp.TcpClient;
import java.net.URISyntaxException;
@Service
public class ImageUploadService {
//日志
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ImageUploadService.class);
private final XxImgfileMapper xxImgfileMapper;
// 创建一个 TcpClient 并设置超时时间
final TcpClient tcpClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) // 连接超时时间
.doOnConnected(connection -> connection.addHandlerLast(new ReadTimeoutHandler(5)) // 读取超时时间
.addHandlerLast(new WriteTimeoutHandler(5))); // 写入超时时间
public ImageUploadService(XxImgfileMapper xxImgfileMapper) {
this.xxImgfileMapper = xxImgfileMapper;
}
......@@ -22,6 +37,7 @@ public class ImageUploadService {
public Integer upload(UploadImageDTO uploadImageDTO) throws NotificationException {
// 创建 WebClient 实例用于下载文件
WebClient webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient)))
.baseUrl(uploadImageDTO.getImagePath())
.build();
// 下载文件到内存中的字节数组
......@@ -30,15 +46,15 @@ public class ImageUploadService {
.retrieve()
.bodyToMono(byte[].class)
.onErrorResume(WebClientResponseException.class, ex -> {
System.err.println("WebClientResponseException: " + ex.getResponseBodyAsString());
log.warn("WebClientResponseException: {}", ex.getResponseBodyAsString());
return Mono.error(new NotificationException(uploadImageDTO.getImagePath() + ",Failed to download file: " + ex.getStatusText()));
})
.onErrorResume(URISyntaxException.class, ex -> {
System.err.println("URISyntaxException: " + ex.getMessage());
log.warn("URISyntaxException: {}", ex.getMessage());
return Mono.error(new NotificationException(uploadImageDTO.getImagePath() + ",Invalid URL format"));
})
.onErrorResume(Exception.class, ex -> {
System.err.println("General Exception: " + ex.getMessage());
log.warn("General Exception: {}", ex.getMessage());
return Mono.error(new NotificationException(ex.getMessage()));
});
......
......@@ -94,7 +94,7 @@ public class MainTest {
Assertions.assertNotNull(secretKey, "appId不存在");//断言appId存在,为空直接抛出异常不进行下一步测试,提高测试效率
//请求参数
UploadImageDTO uploadImageDTO = new UploadImageDTO();
uploadImageDTO.setImagePath("");
uploadImageDTO.setImagePath("http://192.168.168.219/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