天天看点

java实现图片和字符串互相转换的代码

java实现图片和字符串互相转换的代码,也就是说java 转换图片为字符串,将字符串转换成图片显示,具体如下:

java 转换图片为字符串,网页代理将字符串转换成图片显示,

nod32 用户名和密码该方法只适用于比较小的图片传输,50k以内:

try{  

        // 将图片转换成字符串  

        file imgfile = new file("f:\\vista.png");  

        fileinputstream fis = new fileinputstream( imgfile );  

        byte[] bytes = new byte[fis.available()];  

        fis.read(bytes);  

        fis.close();  

        string imgstr = byte2hex( bytes );  

        //system.out.println( imgstr);              

        // 将字符串转换成二进制,用于显示图片  

        byte[] imgbyte = hex2byte( imgstr );  

        inputstream in = new bytearrayinputstream( imgbyte );  

        byte[] b = new byte[1024];  

        int nread = 0;  

        outputstream o = response.getoutputstream();  

        while( ( nread = in.read(b) ) != -1 ){  

            o.write( b, 0, nread );  

        }  

        o.flush();  

        o.close();  

        in.close();  

    }catch(exception e){  

        e.printstacktrace();  

    }