图片流转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);
        }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值