ToB企服应用市场:ToB评测及商务社交产业平台

标题: Java如何使用 HTTP 请求下载图片 [打印本页]

作者: 悠扬随风    时间: 2024-8-12 05:14
标题: Java如何使用 HTTP 请求下载图片
工具类:

  1.     public FileInputStream fileDownload(String fileLink) throws Exception {
  2.         System.out.println("==============开始下载"+fileLink);
  3.         // 转码中文
  4.         URL url = new URL(encodeURLChinese(fileLink));
  5.         System.out.println("fileLink:======================"+url);
  6.         // 开始下载
  7.         TrustStrategy acceptingTrustStrategy = (chain, authType) -> true;
  8.         SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
  9.         SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
  10.         HttpClientBuilder clientBuilder = HttpClients.custom();
  11.         CloseableHttpClient httpClient = clientBuilder.setSSLSocketFactory(sslsf).build();
  12.         HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
  13.         httpRequestFactory.setConnectTimeout(30000);
  14.         httpRequestFactory.setConnectionRequestTimeout(30000);
  15.         httpRequestFactory.setReadTimeout(30000);
  16.         httpRequestFactory.setHttpClient(httpClient);
  17.         RestTemplate template = new RestTemplate(httpRequestFactory);
  18.         // 避免二次转码
  19.         DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory();
  20.         uriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);
  21.         template.setUriTemplateHandler(uriBuilderFactory);
  22.         HttpHeaders headers = new HttpHeaders();
  23.         HttpEntity<org.springframework.core.io.Resource> httpEntity = new HttpEntity<org.springframework.core.io.Resource>(headers);
  24.         ResponseEntity<byte[]> response = template.exchange(url.toString(), HttpMethod.GET,
  25.                 httpEntity, byte[].class);
  26.         byte[] body = response.getBody();
  27.         // 创建临时文件
  28.         File tempFile = File.createTempFile("temp", ".png");
  29.         // 将 byte[] 写入临时文件
  30.         try (FileOutputStream fos = new FileOutputStream(tempFile)) {
  31.             if (body != null) {
  32.                 fos.write(body);
  33.             }
  34.         }
  35.         // 创建 FileInputStream 对象
  36.         FileInputStream stream = new FileInputStream(tempFile);
  37.         System.out.println("=============下载结束");
  38.         // 最后,记得在使用完后删除临时文件
  39.         tempFile.delete();
  40.         return stream;
  41.     }
  42.     public  String encodeURLChinese(String url) {
  43.         if (StringUtils.isEmpty(url)) {
  44.             return null;
  45.         }
  46.         url = StringUtils.trim(url);
  47.         try {
  48.             if (!needEncoding(url)) {
  49.                 // 不需要编码
  50.                 return url;
  51.             } else {
  52.                 // 需要编码
  53.                 String allowChars = ".!*'();:@&=+_\\-$,/?#\\[\\]{}|\\^~`<>%"";
  54. //              String  allowChars = ".!*'();:@&=+_\\-$,/?#\\[\\]{}|\\^~`<>%"";
  55.                 // UTF-8 大写
  56.                 return encode(url, "UTF-8", allowChars, false);
  57.             }
  58.         } catch (Exception e) {
  59.             throw new RuntimeException(e);
  60.         }
  61.     }
复制代码
测试代码

  1.     @GetMapping("export")
  2.     public WebResult<?> exportTeaAndStu() {
  3.         try {
  4.             String url = "https://6c6f-lowcode-9g7kjvsl444eff34-1304647831.tcb.qcloud.la/weda-uploader/7cafc8106cb7cddf27b11dc5c8673562-imageaaa.png?sign=70c4c8a6372fb8b8c743b996a4d1e7b0&t=1723021617";
  5.             String aa = "http://localhost:1401/weda/myLecture/main/speaker/getPhotoByPhotoName?photoName=45550120240510094227.png";
  6.             String fileName = "777";
  7.             //通过链接返回一个输入流
  8.             FileInputStream stream = fileDownload(url);
  9.             minioUtil.uploadInputStream("mpbucket", "sjs/wdjz/zjrgl" + "/" + fileName + ".png", stream);
  10.             return WebResult.ok();
  11.         } catch (Exception e) {
  12.             return WebResult.error(e.getMessage());
  13.         }
  14.     }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4