1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Java实现base64码转换成图片保存到指定地址

Java实现base64码转换成图片保存到指定地址

时间:2022-11-14 15:39:47

相关推荐

Java实现base64码转换成图片保存到指定地址

这是小编在工作是遇到的base64码转换为图片保存到本地路径里的问题,也不是很难,但是对于新手比较不太友好,所以,随手包装成了工具类,希望可以帮助到大家

package com.zhtmips.shebie.util;import sun.misc.BASE64Decoder;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.text.SimpleDateFormat;import java.util.Date;public class ToImgUtil {private String base;public ToImgUtil() {}public ToImgUtil(String base) {this.base = base;}public String getBase() {return base;}public void setBase(String base) {this.base = base;}/*** 为文件重新命名,命名规则为当前系统时间毫秒数** @return string*/private static String getFileNameNew() {SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");return fmt.format(new Date());}public ToImgUtil ToImg(String base){//获取年月日SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//将年、月、日存入数组String[] time = sdf.format(new Date()).split("-");// 文件保存地址 默认地址为xxxx/年/月/日String destDir = "xxxx/"+time[0]+"/"+time[1]+"/"+time[2];;/* response.setHeader("Access-Control-Allow-Origin", "*");response.setHeader("Access-Control-Allow-Methods", "POST");*/base=base.replaceAll("data:image/png;base64,","");BASE64Decoder decoder = new BASE64Decoder();byte[] imageByte = null;try{imageByte = decoder.decodeBuffer(base);for (int i = 0; i < imageByte.length; ++i) {if (imageByte[i] < 0) {// 调整异常数据imageByte[i] += 256;}}} catch (Exception e) {e.printStackTrace();}if (imageByte.length>0) {try {String filepath = destDir;File destfile = new File( filepath);if (!destfile.exists()) {destfile.mkdirs();}//文件新名称String fileNameNew = getFileNameNew() + ".png";File f = new File(destfile.getAbsoluteFile() + File.separator + fileNameNew);// 将字符串转换成二进制,用于显示图片// 将上面生成的图片格式字符串 imgStr,还原成图片显示InputStream in = new ByteArrayInputStream(imageByte);FileOutputStream fos = new FileOutputStream(f);// BufferedOutputStream bos = new BufferedOutputStream(fos);byte[] buf = new byte[1024];int length;length = in.read(buf, 0, buf.length);while (length != -1) {fos.write(buf,0,length);length = in.read(buf);}fos.flush();fos.close();in.close();return new ToImgUtil("图片地址:"+destDir+",图片名称"+fileNameNew);} catch (Exception e) {e.printStackTrace();} finally {}}return null;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。