且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-06-01 11:56:05

 

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();  
    }