代码如下:
package com.x3.gls.util;
import com.x3.base.core.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.Base64;
/**
* 图片远程下载
*/
@Component
public class RemoteImageUtil {
/**
* 日志
*/
private static final Logger log = LoggerFactory.getLogger(HttpPostUtil.class);
/**
* 图片远程下载并转换为Base64
*
* @param imageUrl
* @return
*/
public static String convertImageUrlToBase64(String imageUrl) {
RestTemplate restTemplate = new RestTemplate();
try {
ResponseEntity<byte[]> responseEntity = restTemplate.getForEntity(imageUrl, byte[].class);
byte[] imageBytes = responseEntity.getBody();
if (imageBytes != null) {
return Base64.getEncoder().encodeToString(imageBytes);
} else {
return null;
}
} catch (Exception e) {
log.debug("RemoteImageUtil-convertImageUrlToBase64-01-异常响应数据:{}", e.getMessage());
throw new BusinessException("RemoteImageUtil-convertImageUrlToBase64-02",
"下载文件[" + imageUrl + "]出错:" + e.getMessage(), imageUrl);
}
}
}字节(Byte)是计量单位,表示数据量多少,是计算机信息技术用于计量存储容量的一种计量单位,通常情况下一字节等于八位。字符(Character)计算机中使用的字母、数字、字和符号,比如'A'、'B'、'$'、'&'等。一般在英文...
public class test { public static void main(String[] args) { &...
java限制1个方法同一时间只能被一个线程访问public synchronized void setOrderPay(){ }加上synchronized 修饰符即可...
Java5 引入了一种主要用于数组的增强型 for 循环,类似js中的for inpublic class Member { public static void main(String[]&...
java判断字符是否是一个字母System.out.println(Character.isLetter('a'));java判断字符是否是一个数字System.out.println(Character.isDigit('0'));java判断字符是否是一个空白Sy...
(1).创建数组double[] myList = new double[size]; //推荐创建方式 double myList[] = new double[size];  ...