天天看點

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

    }