com.cntool.core.Imag.ImgUtils Maven / Gradle / Ivy
The newest version!
package com.cntool.core.Imag;
import com.cntool.core.string.StrUtils;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Base64;
import static com.cntool.core.Imag.ImgUtils.toBase64;
/**
* @version 1.1.0
* @program cntool
* @ClassName ImagUtils
* @description: 图片工具类
* @author: ID-Tang
* @create: 2022-08-31 9:48
* @copyright: Copyright (c) [2022] [ID-tang]
* [cntool] is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* ...
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
**/
public class ImgUtils {
/**
* 网络图片下载(无返回值)
*
* @param imagUrl 网络图片地址
* @param filePath 下载位置
* @throw NullPointerException 网络图片位置或图片下载位置为空时,抛出该空指针异常
* @since 0.0.8
*/
public static void httpUpload(String imagUrl, String filePath) {
if (StrUtils.isBlank(imagUrl)) {
throw new NullPointerException("网络图片位置不能为空");
}
if (StrUtils.isBlank(filePath)) {
throw new NullPointerException("图片下载位置不能为空");
}
Methods.upload(imagUrl, filePath);
}
/**
* 本地图片转Base64
*
* @param filePath 图片路径
* @return Base64编码
* @since 0.0.8
*/
public static String toBase64(String filePath) {
//获取Base64编码器
Base64.Encoder encoder = Base64.getEncoder();
//数据集缓存器
byte[] imgContainer;
//文件输入流
FileInputStream fileInputStream;
try {
//到指定路径寻找文件
fileInputStream = new FileInputStream(filePath);
//设置图片字节数据缓冲区大小
imgContainer = new byte[fileInputStream.available()];
//将数据流中的图片数据读进缓冲区
fileInputStream.read(imgContainer);
//将图片编码转换成Base64格式的数据集
String base64ImgData = encoder.encodeToString(imgContainer);
//关闭数据流
fileInputStream.close();
//将缓冲区数据转换成字符数据返回
return base64ImgData;
} catch (FileNotFoundException e) {
return "找不到指定文件!";
} catch (IOException e) {
e.printStackTrace();
}
return "null";
}
/**
* 网络图片编码为base64
*
* @param imgUrl 网络图片地址
* @return base64字符串
* @since 1.1.0
*/
public static String httpToBase64(String imgUrl) {
try {
URL url = new URL(imgUrl);
//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
System.out.println("图片的路径为:" + url);
//打开链接
HttpURLConnection conn;
conn = (HttpURLConnection) url.openConnection();
//设置请求方式为"GET"
conn.setRequestMethod("GET");
//超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
//通过输入流获取图片数据
InputStream inStream = conn.getInputStream();
//得到图片的二进制数据,以二进制封装得到数据,具有通用性
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//创建一个Buffer字符串
byte[] buffer = new byte[1024];
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int len;
//使用一个输入流从buffer里把数据读取出来
while ((len = inStream.read(buffer)) != -1) {
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
//关闭输入流
inStream.close();
byte[] data = outStream.toByteArray();
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
String base64 = encoder.encode(data);
System.out.println("网络文件[{}]编码成base64字符串:[{}]" + url + base64);
//返回Base64编码过的字节数组字符串
return base64;
} catch (IOException e) {
e.printStackTrace();
System.out.println("图片转换失败!");
return null;
}
}
}
class Methods {
/**
* 网络图片下载
*
* @param imagUrl 网络图片地址
* @param filePath 下载位置
* @since 0.0.8
*/
static void upload(String imagUrl, String filePath) {
URL url;
URLConnection conn;
InputStream inputStream = null;
FileOutputStream out = null;
try {
url = new URL(imagUrl);
conn = url.openConnection();
inputStream = conn.getInputStream();
File file = new File(filePath);
byte[] buffer = new byte[1024];
int len;
out = new FileOutputStream(file);
while ((len = inputStream.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//关闭输入流
if (inputStream != null) {
inputStream.close();
}
//关闭输出流
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
static void main(String[] args) {
System.out.println(toBase64("E:/download/de9492de-a7b9-420e-8025-b7c26b3921ac.jpg"));
}
}