代码如下:
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);
}
}
}public class test { public static void main(String[] args) { &...
Java5 引入了一种主要用于数组的增强型 for 循环,类似js中的for inpublic class Member { public static void main(String[]&...
在java中字符串属于对象,刚开始我就疑惑为什么int char等类型都是小写,结果String是大写,java太反人类,后来才知道String是对象。(1).java创建字符串String text = "net"; String tex...
(1).java睡眠函数Thread.sleep(时间); //单位为毫秒(2).java睡眠函数例子Date dNow = new Date(); SimpleDateFormat ft = new&nbs...
System.out.println("当前时间戳(秒): " + System.currentTimeMillis()/1000); System.out.println("当前时间戳(毫秒): " +&nb...
(4).java lookingAt匹配字符串和java matches匹配字符串lookingAt不要求整个字符串全匹配,例如me和me_you都是匹配的,但是lookingAt从第一个字符串开始匹配,匹配失败了也不会继续匹配,意味着me和you_me是无法匹配的matches匹配字符串要求全部匹...