IntelligentRecognition/ah-jjsp-service/.svn/pristine/09/09bbb0bb3c4f00eec06c64c5a22...

154 lines
4.8 KiB
Plaintext

package com.sercurityControl.proteam.util;
import com.securityControl.common.core.utils.aes.StringHelper;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import java.util.Objects;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @author HeiZi
*/
@Slf4j
public class BinaryStreamReceiver {
public static String transImage(String url){
try{
byte[] binaryData=getImage2(url);
try (ByteArrayInputStream bis = new ByteArrayInputStream(binaryData)) {
// 从输入流中读取图像
BufferedImage image = ImageIO.read(bis);
if (image != null) {
File folder = new File("/data/ahsbs/file/wz");
if (!folder.exists()) {
boolean mkdir= folder.mkdir();
}
// 保存图像到指定路径
String outputPath = "/data/ahsbs/file/wz/image.jpg";
File outputFile = new File(outputPath);
ImageIO.write(image, "jpg", outputFile);
String bast64=convertImageToBase64Str(outputPath);
if(StringHelper.isNotEmpty(bast64)){
deleteFile(outputPath);
}
return bast64;
} else {
// System.err.println("无效的二进制数据流或不支持的格式!");
}
} catch (IOException e) {
// System.err.println("发生错误:" + e.getMessage());
}
}catch (Exception e){
log.error(e.toString(),e);
}
return null;
}
public static byte[] getImage2(String url) throws IOException {
HttpURLConnection connection = null;
InputStream inputStream = null;
try {
URL apiUrl = new URL(url);
connection = (HttpURLConnection) apiUrl.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
// log.info("请求成功了----->");
inputStream = connection.getInputStream();
return readBytesFromInputStream(inputStream);
} else {
// log.info("请求失败了----->");
throw new IOException("请求异常 " + responseCode);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (connection != null) {
connection.disconnect();
}
}
}
private static byte[] readBytesFromInputStream(InputStream inputStream) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] data = new byte[4096];
int n;
while ((n = inputStream.read(data)) != -1) {
buffer.write(data, 0, n);
}
buffer.flush();
return buffer.toByteArray();
}
/**
* 删除单个文件
*
* @param sPath 被删除文件的文件名
*/
public static void deleteFile(String sPath) {
boolean result = false;
try {
File file = new File(sPath);
if (file.isFile() && file.exists()) {
int tryCount = 0;
while (!result && tryCount++ < 10) {
System.gc();
result = file.delete();
}
}
} catch (Exception e) {
log.error("文件删除异常", e);
}
}
/**
* 图片转Base64码
* @param src
* @return
*/
public static String convertImageToBase64Str(String src) {
ByteArrayOutputStream baos = null;
try {
String suffix = src.substring(src.lastIndexOf(".") + 1);
File imageFile = new File(src);
BufferedImage bufferedImage = ImageIO.read(imageFile);
baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, suffix, baos);
byte[] bytes = baos.toByteArray();
return Base64.getEncoder().encodeToString(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}