java网络图片转base64_Java 从网络中读取图片 转换成Base64字符串

该博客介绍了如何使用Java从网络上读取图片并将其转换为Base64字符串。通过示例代码展示了从URL获取图片流,再将流转换为Base64编码的过程,同时也提供了将Base64字符串还原为图片的方法。代码中注意到了HttpURLConnection关闭时机的问题,避免影响到流的读取。
摘要由CSDN通过智能技术生成

有一个小功能 , 从网络上读取图片 , 然后将图片转换为String类型,发给别人.

废话不多说了,直接上代码.

这里附上完整代码....有个jar包 , 不是那么好找,已经放在zip包中了....

我本地测试的时候,用的是Tomcat服务器.

对了,这里发现一个小bug,要是在try..catch之后,

直接finally , 将HttpSRLConnection关闭的话,读取的InputStream 就是空了...打断点看了也没想明白...欢迎大家交流.

然后我想了一下 , 将HttpURLConnection httpUrl 设置为成员变量 , 放在外面 , 每次返回结果之后 , 在调用close方法 , 单独讲这个连接关闭了.

package com.readerpicture;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import Decoder.BASE64Decoder;

import Decoder.BASE64Encoder;

public class Controller {

private HttpURLConnection httpUrl = null;

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

Controller c = new Controller();

String url = "http://localhost:8088/tomcat.png";

String path = "D:/0000.JPG";

InputStream in = c.saveToFile(url);//从URL读取图片

String str = c.GetImageStrByInPut(in);//读取输入流,转换为Base64字符

System.out.println(str);

generateImage(str, path);//将Base64字符转换为图片

c.closeHttpConn();

}

public void closeHttpConn(){

httpUrl.disconnect();

}

/**

* 从URL中读取图片,转换成流形式.

* @param destUrl

* @return

*/

public InputStream saveToFile(String destUrl){

URL url = null;

InputStream in = null;

try{

url = new URL(destUrl);

httpUrl = (HttpURLConnection) url.openConnection();

httpUrl.connect();

httpUrl.getInputStream();

in = httpUrl.getInputStream();

return in;

}catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

* 读取输入流,转换为Base64字符串

* @param input

* @return

*/

public String GetImageStrByInPut(InputStream input) {

byte[] data = null;

// 读取图片字节数组

try {

data = new byte[input.available()];

input.read(data);

input.close();

} catch (IOException e) {

e.printStackTrace();

}

// 对字节数组Base64编码

BASE64Encoder encoder = new BASE64Encoder();

return encoder.encode(data);// 返回Base64编码过的字节数组字符串

}

/**

* 图片转化成base64字符串 将图片文 件转化为字节数组字符串,并对其进行Base64编码处理

*

* @return

*/

public static String GetImageStr(File file) {

InputStream in = null;

byte[] data = null;

// 读取图片字节数组

try {

in = new FileInputStream(file);

data = new byte[in.available()];

in.read(data);

in.close();

} catch (IOException e) {

e.printStackTrace();

}

// 对字节数组Base64编码

BASE64Encoder encoder = new BASE64Encoder();

return encoder.encode(data);// 返回Base64编码过的字节数组字符串

}

/**

* base64字符串转化成图片 对字节数组字符串进行Base64解码并生成图片

*

* @param imgStr

* 数据内容(字符串)

* @param path

* 输出路径

* @return

*/

public static boolean generateImage(String imgStr, String path) {

if (imgStr == null) // 图像数据为空

return false;

BASE64Decoder decoder = new BASE64Decoder();

try {

byte[] b = decoder.decodeBuffer(imgStr);// Base64解码

for (int i = 0; i < b.length; ++i) {

if (b[i] < 0) {// 调整异常数据

b[i] += 256;

}

}

// 生成jpeg图片

OutputStream out = new FileOutputStream(path);

out.write(b);

out.flush();

out.close();

return true;

} catch (Exception e) {

return false;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值