我做了一个 对网络图片的流 进行base64 然后在进行解代码代码如下 public String getImage(String imgurl) throws IOException {
String res = "";
java.io.BufferedInputStream bis = null;
try {
URL url = new URL(imgurl);
bis = new BufferedInputStream(url.openStream()); byte data[] = new byte[bis.available()];
int i = 0;
while ((i = bis.read(data)) != -1)
{
bis.read(data, 0, i);
}
System.out.println(data.length);
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
res = encoder.encode(data);
//对字节数组Base64编码
// BASE64Encoder encoder = new BASE64Encoder();
// res = encoder.encode(data);//返回Base64编码过的字节数组字符串
} catch (Exception ex) {
Logger.getLogger(ActivitiesAction.class.getName()).log(Level.SEVERE, null, ex);
} finally {
bis.close();
}
return res;
}
//将base64后的转换为图片
public static void baseTochangeImage(String base64img) {
try {
byte[] result = new sun.misc.BASE64Decoder().decodeBuffer(base64img.trim());
RandomAccessFile inOut = new RandomAccessFile("d:\\img\\1.jpg", "rw"); // r,rw,rws,rwd
inOut.write(result);
inOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}有些小的图片可以 但是图片要是30多K的好像就不能正确读取了 我跟踪了一下程序 好像是
byte data[] = new byte[bis.available()]; 这句话中的bis.available() 这个的问题不真知道如何解决 希望高手指点 多谢
String res = "";
java.io.BufferedInputStream bis = null;
try {
URL url = new URL(imgurl);
bis = new BufferedInputStream(url.openStream()); byte data[] = new byte[bis.available()];
int i = 0;
while ((i = bis.read(data)) != -1)
{
bis.read(data, 0, i);
}
System.out.println(data.length);
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
res = encoder.encode(data);
//对字节数组Base64编码
// BASE64Encoder encoder = new BASE64Encoder();
// res = encoder.encode(data);//返回Base64编码过的字节数组字符串
} catch (Exception ex) {
Logger.getLogger(ActivitiesAction.class.getName()).log(Level.SEVERE, null, ex);
} finally {
bis.close();
}
return res;
}
//将base64后的转换为图片
public static void baseTochangeImage(String base64img) {
try {
byte[] result = new sun.misc.BASE64Decoder().decodeBuffer(base64img.trim());
RandomAccessFile inOut = new RandomAccessFile("d:\\img\\1.jpg", "rw"); // r,rw,rws,rwd
inOut.write(result);
inOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}有些小的图片可以 但是图片要是30多K的好像就不能正确读取了 我跟踪了一下程序 好像是
byte data[] = new byte[bis.available()]; 这句话中的bis.available() 这个的问题不真知道如何解决 希望高手指点 多谢
{
bis.read(data, 0, i);
}觉得这段有问题。这要这一句:bis.read(data);即可。
i = bis.read(data)这句已使bis到达末尾了。