根据图片地址转换base64码并输出本地

public static void main(String[] args) throws IOException {

    String urlPath = "\n" +
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1592375119711&di=df41052a64fbb26a80b96368aa63802f&imgtype=0&src=http%3A%2F%2Fgbres.dfcfw.com%2FFiles%2Fpicture%2F20200519%2FA470FA6879010F8F0A5EE64267367E5E_w640h512.jpg\n" +
            "\n";

// String imgData = new sun.misc.BASE64Encoder().encode(Img_url.getBytes());
ByteArrayOutputStream data = new ByteArrayOutputStream();
try {
URL url = new URL(urlPath);
System.out.println(“url:” + url);
byte[] by = new byte[1024];
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
httpURLConnection.setConnectTimeout(1000 * 5);
httpURLConnection.connect();
InputStream inputStream = httpURLConnection.getInputStream();
int len = -1;
while ((len = inputStream.read(by)) != -1) {
data.write(by, 0, len);
}
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
String imgData = Base64.getMimeEncoder().encodeToString(data.toByteArray());
System.out.println(“imgData:” + imgData);
OutputStream out = null;
//解码输出本地
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = decoder.decodeBuffer(imgData);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
out = new FileOutputStream(“D:\人脸信息图片.jpg”);
out.write(b);
out.flush();
out.close();

}

你可能感兴趣的:(转码,java,java,base64)