您的位置:首页 > 科技 > 能源 > Java如何使用 HTTP 请求下载图片

Java如何使用 HTTP 请求下载图片

2024/9/29 22:14:52 来源:https://blog.csdn.net/XikYu/article/details/140997099  浏览:    关键词:Java如何使用 HTTP 请求下载图片

工具类:

    public FileInputStream fileDownload(String fileLink) throws Exception {System.out.println("==============开始下载"+fileLink);// 转码中文URL url = new URL(encodeURLChinese(fileLink));System.out.println("fileLink:======================"+url);// 开始下载TrustStrategy acceptingTrustStrategy = (chain, authType) -> true;SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);HttpClientBuilder clientBuilder = HttpClients.custom();CloseableHttpClient httpClient = clientBuilder.setSSLSocketFactory(sslsf).build();HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();httpRequestFactory.setConnectTimeout(30000);httpRequestFactory.setConnectionRequestTimeout(30000);httpRequestFactory.setReadTimeout(30000);httpRequestFactory.setHttpClient(httpClient);RestTemplate template = new RestTemplate(httpRequestFactory);// 避免二次转码DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory();uriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);template.setUriTemplateHandler(uriBuilderFactory);HttpHeaders headers = new HttpHeaders();HttpEntity<org.springframework.core.io.Resource> httpEntity = new HttpEntity<org.springframework.core.io.Resource>(headers);ResponseEntity<byte[]> response = template.exchange(url.toString(), HttpMethod.GET,httpEntity, byte[].class);byte[] body = response.getBody();// 创建临时文件File tempFile = File.createTempFile("temp", ".png");// 将 byte[] 写入临时文件try (FileOutputStream fos = new FileOutputStream(tempFile)) {if (body != null) {fos.write(body);}}// 创建 FileInputStream 对象FileInputStream stream = new FileInputStream(tempFile);System.out.println("=============下载结束");// 最后,记得在使用完后删除临时文件tempFile.delete();return stream;}public  String encodeURLChinese(String url) {if (StringUtils.isEmpty(url)) {return null;}url = StringUtils.trim(url);try {if (!needEncoding(url)) {// 不需要编码return url;} else {// 需要编码String allowChars = ".!*'();:@&=+_\\-$,/?#\\[\\]{}|\\^~`<>%\"";
//              String  allowChars = ".!*'();:@&=+_\\-$,/?#\\[\\]{}|\\^~`<>%\"";// UTF-8 大写return encode(url, "UTF-8", allowChars, false);}} catch (Exception e) {throw new RuntimeException(e);}}

测试代码

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com