栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

图片流转base64遇到的坑

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

图片流转base64遇到的坑

Java学习问题2:网络传输的图片转base64遇到的坑

1.之前写图片转base64流的写法

		File file = new File("D:\test\img11.jpg");
        InputStream inputStream = null;
        BufferedOutputStream bos = null;
        FileOutputStream outputStream = null;
        try {
            //读取文件转图片base64流
            inputStream = new  FileInputStream(file);
            //inputStream.available()可以在读写操作前提前得知数据流有多少字节,初始化byte数组
            byte[] image = new byte[inputStream.available()];
            int readBytes = 0;
            inputStream.read(image);
            //将图片转为base64流
            base64Encoder encoder = new base64Encoder();
            String imageString = encoder.encode(image);


            //将base64流转图片
            File dir = new File("D:\test");
            if(!dir.exists() && dir.isDirectory()){
                dir.mkdir();
            }
            base64Decoder decoder = new base64Decoder();
            byte[] outByte = decoder.decodeBuffer(imageString);
            File outFile =  new File("D:\test\test1.jpg");
            outputStream = new FileOutputStream(outFile);
            bos = new BufferedOutputStream(outputStream);
            bos.write(outByte);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }

但是inputStream.available()在网络传输过程中,很有可能返回的是0,非常不靠谱!基本网上都是这个写法,在本地无论测试多少遍都是ok的。

2.不应该定义数组大小,而是交给缓存数组遍历while来做,修改后的代码如下。

		InputStream in = null;
        String imageString = null;
        byte[] image = null;
        try {
            in = new FileInputStream("F:\xsnasnew\groupvisit\actword\test.jpg");
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int n = 0;
            while (-1 != (n = in.read(buffer))) {
                output.write(buffer, 0, n);
            }
            image = output.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if(image != null && image.length > 0){
            base64Encoder encoder = new base64Encoder();
            imageString = encoder.encode(image);
        }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/602821.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号