java图片转byte转string


第一种:原始乱码:

public static void main(String[] args) throws IOException {
            File imgFile = new File("d:\\1.jpg");   
            FileInputStream fis = new FileInputStream( imgFile );   
            byte[] bytes = new byte[fis.available()];   
            fis.read(bytes);   
            fis.close();   
            String imgStr = new String(bytes);   
            System.out.println( imgStr);      
    }

第二种:完整字节:

public String byteToString(byte[] buf){
        String str1="";
        StringBuilder sb=new StringBuilder(str1);
        for (byte element: buf )
        {
        sb.append(element);
        }
        str1=sb.toString();
        return str1;
    }
    public static void main(String[] args) throws IOException {
            File imgFile = new File("d:\\1.jpg");   
            FileInputStream fis = new FileInputStream( imgFile );   
            byte[] bytes = new byte[fis.available()];   
            fis.read(bytes);   
            fis.close();   
            String imgStr = new Byte().byteToString(bytes);   
            System.out.println( imgStr);      
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM