Kaynağa Gözat

download fix

wangyunpeng 1 hafta önce
ebeveyn
işleme
c612f5152a

+ 2 - 0
src/main/java/com/tzld/ffmpeg/service/impl/ImageServiceImpl.java

@@ -84,6 +84,7 @@ public class ImageServiceImpl implements ImageService {
             OkHttpClient client = new OkHttpClient();
             Request request = new Request.Builder()
                     .url(CdnUtil.getOssHttpUrl(imageUrl))
+                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
                     .build();
             try (Response response = client.newCall(request).execute()) {
                 if (response.isSuccessful()) {
@@ -102,6 +103,7 @@ public class ImageServiceImpl implements ImageService {
         try {
             Request request = new Request.Builder()
                     .url(CdnUtil.getOssHttpUrl(imageUrl))
+                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
                     .build();
             try (Response response = okHttpClient.newCall(request).execute()) {
                 if (response.isSuccessful()) {

+ 1 - 1
src/main/java/com/tzld/ffmpeg/util/CdnUtil.java

@@ -101,7 +101,7 @@ public class CdnUtil {
         if (StringUtils.isBlank(key)) {
             return key;
         }
-        if (org.springframework.util.StringUtils.startsWithIgnoreCase(key, "http")) {
+        if (StringUtils.startsWithIgnoreCase(key, "http")) {
             return key;
         } else {
             String url = DOWNLOAD_CDN_URL_HOST_PICTURE + key;

+ 29 - 0
src/test/java/ImageServiceTest.java

@@ -0,0 +1,29 @@
+
+import com.tzld.ffmpeg.Application;
+import com.tzld.ffmpeg.exception.DownloadImageError;
+import com.tzld.ffmpeg.service.ImageService;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import java.util.List;
+
+@SpringBootTest(classes = Application.class)
+@Slf4j
+public class ImageServiceTest {
+
+    @Autowired
+    private ImageService imageService;
+
+    @Test
+    public void test() {
+        String url = "http://rescdn.yishihui.com/temp/1763018780183_TVmO96wlvOPiFEtqWy.png";
+        try {
+            byte[] bytes = imageService.imageDownloadV2(url);
+            log.info("下载图片成功");
+        } catch (DownloadImageError e) {
+            log.error("下载图片失败", e);
+        }
+    }
+}