mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add methods
This commit is contained in:
@@ -13,6 +13,7 @@ import cn.hutool.core.util.NumberUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
|
||||
import javax.imageio.IIOImage;
|
||||
import javax.imageio.ImageIO;
|
||||
@@ -1261,6 +1262,20 @@ public class ImgUtil {
|
||||
return IoUtil.toStream(toBytes(image, imageType));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将图片对象转换为Base64的Data URI形式,格式为:data:image/[imageType];base64,[data]
|
||||
*
|
||||
* @param image 图片对象
|
||||
* @param imageType 图片类型
|
||||
* @return Base64的字符串表现形式
|
||||
* @since 5.3.6
|
||||
*/
|
||||
public static String toBase64DateUri(Image image, String imageType) {
|
||||
return URLUtil.getDataUri(
|
||||
"image/" + imageType, "base64",
|
||||
toBase64(image, imageType));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将图片对象转换为Base64形式
|
||||
*
|
||||
|
@@ -28,7 +28,16 @@ import java.util.Map;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
/**
|
||||
* 统一资源定位符相关工具类
|
||||
* URL(Uniform Resource Locator)统一资源定位符相关工具类
|
||||
*
|
||||
* <p>
|
||||
* 统一资源定位符,描述了一台特定服务器上某资源的特定位置。
|
||||
* </p>
|
||||
* URL组成:
|
||||
* <pre>
|
||||
* 协议://主机名[:端口]/ 路径/[:参数] [?查询]#Fragment
|
||||
* protocol :// hostname[:port] / path / [:parameters][?query]#fragment
|
||||
* </pre>
|
||||
*
|
||||
* @author xiaoleilu
|
||||
*/
|
||||
@@ -749,11 +758,11 @@ public class URLUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
* @since 5.3.4
|
||||
*/
|
||||
public static long getContentLength(URL url) throws IORuntimeException{
|
||||
if(null == url){
|
||||
public static long getContentLength(URL url) throws IORuntimeException {
|
||||
if (null == url) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
URLConnection conn = null;
|
||||
try {
|
||||
conn = url.openConnection();
|
||||
@@ -762,8 +771,61 @@ public class URLUtil {
|
||||
throw new IORuntimeException(e);
|
||||
} finally {
|
||||
if (conn instanceof HttpURLConnection) {
|
||||
((HttpURLConnection)conn).disconnect();
|
||||
((HttpURLConnection) conn).disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data URI Scheme封装。data URI scheme 允许我们使用内联(inline-code)的方式在网页中包含数据,<br>
|
||||
* 目的是将一些小的数据,直接嵌入到网页中,从而不用再从外部文件载入。常用于将图片嵌入网页。
|
||||
*
|
||||
* <p>
|
||||
* Data URI的格式规范:
|
||||
* <pre>
|
||||
* data:[<mime type>][;charset=<charset>][;<encoding>],<encoded data>
|
||||
* </pre>
|
||||
*
|
||||
* @param mimeType 可选项(null表示无),数据类型(image/png、text/plain等)
|
||||
* @param encoding 数据编码方式(US-ASCII,BASE64等)
|
||||
* @param data 编码后的数据
|
||||
* @return Data URI字符串
|
||||
* @since 5.3.6
|
||||
*/
|
||||
public static String getDataUri(String mimeType, String encoding, String data){
|
||||
return getDataUri(mimeType, null, encoding, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data URI Scheme封装。data URI scheme 允许我们使用内联(inline-code)的方式在网页中包含数据,<br>
|
||||
* 目的是将一些小的数据,直接嵌入到网页中,从而不用再从外部文件载入。常用于将图片嵌入网页。
|
||||
*
|
||||
* <p>
|
||||
* Data URI的格式规范:
|
||||
* <pre>
|
||||
* data:[<mime type>][;charset=<charset>][;<encoding>],<encoded data>
|
||||
* </pre>
|
||||
*
|
||||
* @param mimeType 可选项(null表示无),数据类型(image/png、text/plain等)
|
||||
* @param charset 可选项(null表示无),源文本的字符集编码方式
|
||||
* @param encoding 数据编码方式(US-ASCII,BASE64等)
|
||||
* @param data 编码后的数据
|
||||
* @return Data URI字符串
|
||||
* @since 5.3.6
|
||||
*/
|
||||
public static String getDataUri(String mimeType, Charset charset, String encoding, String data){
|
||||
final StringBuilder builder = StrUtil.builder("data:");
|
||||
if(StrUtil.isNotBlank(mimeType)){
|
||||
builder.append(mimeType);
|
||||
}
|
||||
if(null != charset){
|
||||
builder.append(";charset=").append(charset.name());
|
||||
}
|
||||
if(StrUtil.isNotBlank(encoding)){
|
||||
builder.append(';').append(encoding);
|
||||
}
|
||||
builder.append(',').append(data);
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user