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