您的位置:首页 > 理论基础 > 计算机网络

Java模块 -- 从网络中读取图片 转换成Base64字符串

2016-08-23 22:46 986 查看
有一个小功能 , 从网络上读取图片 , 然后将图片转换为String类型,发给别人.

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

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

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

我本地测试的时候,用的是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;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: