CustomStringUtil



package com.test.util;

import org.apache.commons.lang.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.*;
import java.util.Calendar;

/**
 * Created by Administrator on 2018/4/10.
 */
public class CustomStringUtil {

    public static String getErrorInfoFromException(Exception e) {
        try {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            return "\r\n" + sw.toString() + "\r\n";
        } catch (Exception e2) {
            return "bad getErrorInfoFromException";
        }
    }

    /**
     * 图片转字符串
     * @return
     */
    public static String getImageBinary(String imagePath,String imageType) {
        BASE64Encoder encoder = new sun.misc.BASE64Encoder();
        File f = new File(imagePath);
        BufferedImage bi;
        try {
            bi = ImageIO.read(f);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bi, imageType, baos);
            byte[] bytes = baos.toByteArray();

            return encoder.encodeBuffer(bytes).trim();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 字符串转图片
     * @param base64String
     */
    public static boolean base64StringToImage(StringBuffer base64String,String toImagePath,String imageType) {
        try {
            BASE64Decoder decoder = new sun.misc.BASE64Decoder();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(base64String.toString().getBytes());
            byte[] bytes1 = decoder.decodeBuffer(byteArrayInputStream);

            ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
            RenderedImage bi1 = ImageIO.read(bais);
            File w2 = new File(toImagePath);// 可以是jpg,png,gif格式
            if(!w2.exists()){
                w2.createNewFile();
                System.out.println("no exist=====");
            }
            System.out.println("pass...........");
            return ImageIO.write(bi1, imageType, w2);// 不管输出什么格式图片,此处不需改动
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取时间随机数
     * @return
     * @throws Exception
     */
    public static String getExtension()throws Exception{
        try {
            //线程睡会
            Thread.sleep(1);
            //生成日期实体
            Calendar calendar = Calendar.getInstance();
            String extension = calendar.getTime().getTime()+"";
            return extension;//专网及EZVP公网
        } catch (InterruptedException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将数组转换为字符串
     * @param array 数组对象
     * @param separator 分割字符
     * @return
     * @throws Exception
     */
    public static String getArrayToString(Object[] array, String separator)throws Exception{
        String resultStr = StringUtils.join(array, separator);
        return resultStr;
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值