转换url:data数据为正常图片

来源:互联网 发布:windows记事本下载 编辑:程序博客网 时间:2024/04/20 09:45
/** * 转换url:data数据为正常图片 * @param imgStr * @param imgName * @param imgPath * @return * by yanqingwen */public static boolean GenerateImage(String imgStr,String imgName,String imgPath){//对Base64字符串解码并生成图片if (imgStr == null){        return false;        } //图像数据为空        BASE64Decoder decoder = new BASE64Decoder();        try{            //Base64解码           byte[] b = decoder.decodeBuffer(imgStr);           for(int i=0;i<b.length;++i){                if(b[i]<0){//调整异常数据b[i]+=256;}           }File headPath = new File(imgPath);//获取文件夹路径 if(!headPath.exists()){//判断文件夹是否创建,没有创建则创建新文件夹 headPath.mkdirs(); }//定义图片路径String imgFilePath = imgPath+"/"+imgName+".jpg";//新生成的图片OutputStream out = new FileOutputStream(imgFilePath);out.write(b);out.flush();out.close();return true;       }catch (Exception e){       return false;}}
这个是做一个头像上传时,本来是保存字符串的,但是保存字符串对于数据库来说压力有些大了,而且字符串要保证不能过长。

因此我做了一个图片的转换器,把Bas64转换为正常的图片保存到服务器。
0 0
原创粉丝点击