public static boolean saveBase64Pic(String imgStr,Integer imgStrLength,String picNewname,String keepFilePath){
//对字节数组字符串进行Base64解码并生成图片
imgStr=imgStr.replaceFirst("data:image/jpeg;base64,", "");
System.out.println("后来:"+imgStr);
if (imgStr == null) //图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try
{
//Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
System.out.println("imgStrLength:"+imgStrLength+",解码后长度:"+b.length);
for(int i=0;i<b.length;++i)
{
if(b[i]<0)
{//调整异常数据
b[i]+=256;
}
}
//生成jpeg图片
String imgFilePath = keepFilePath+"\\"+picNewname;//新生成的图片
//String imgFilePath ="e://222.jpg";
System.out.println(imgFilePath);
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
}
catch (Exception e)
{
return false;
}
}
解决方案 »
- 在线给分!<pre>中如何不让全角“—”自动变为半角“-”
- 急急急急急急急急急急
- 文件上传程序
- 请大师解决这个的执行结果,急,在线等!以下使用java技术执行后的结果?
- web结构与性能问题
- 小妹求一简单问题,烦劳各位回答详细点:用Applet访问Sql server2000并执行查询。
- 打印
- jsp里实现新消息提醒,有什么好的思路?
- 急!!!
- package filename是什么东西呀,是不是要不要都无所谓的?
- /handlelogon.jsp (line: 11, column: 1) The value for the useBean class attribute
- 求一个AJAX连接SQL数据库实现界面无刷新的增删改查项目
String str = text.substring(text.indexOf(",")+1);
因为base64图片的前缀是需要去掉的,网上的大多数没说这个问题。截取下数据就好了
楼主已经做了这个操作了:imgStr=imgStr.replaceFirst("data:image/jpeg;base64,", "");
使用这个方法试试吧,亲测,没有问题: public static boolean generateImage(String base64Code, String path) {
if (null == base64Code) {
return false;
}
byte[] base64Byte = Base64.decodeBase64(base64Code);
try {
OutputStream out = new FileOutputStream(path);
out.write(base64Byte);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
其中Base64 是导入的这个包org.apache.commons.codec.binary.Base64注意:也得吧前缀data:image/jpeg;base64,去掉
System.out.println("前面:"+imgStr); // 前后确认下 有些图可能不是 jpg,
imgStr=imgStr.replaceFirst("data:image/jpeg;base64,", "");
System.out.println("后来:"+imgStr);
// 这个操作没有必要
for(int i=0;i<b.length;++i)
{
if(b[i]<0)
{//调整异常数据
b[i]+=256;
}
}