JAVA将⽹络照⽚转base64并压缩  <dependency>
<groupId&bird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
bird.thumbnailator.Thumbnails;
import dec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.HttpURLConnection;
import java.URL;
public class ImageService {
/**
* 将⽹络图⽚编码为base64
*
* @return
* @throws
*/
public static String factoryBase(String StoragePath) {
try {
URL url = new URL(StoragePath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = InputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = ad(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
byte[] data = ByteArray();
BASE64Encoder encoder = new BASE64Encoder();
String base64 = de(data);
return base64;
} catch (Exception e) {
return "";
}
java stream
}
public static String base64Image(String url) {
if (url.indexOf("http") > -1) {
String base64 = factoryBase(url);
if (base64 != "") {
return resizeImageTo40K(base64);
}
}
return "";
}
public static BufferedImage base64String2BufferedImage(String base64string) {
BufferedImage image = null;
try {
InputStream stream = BaseToInputStream(base64string);
image = ad(stream);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。