JAVA NIO读取网络图片问题

本文介绍使用Java NIO读取网络图片并保存至本地的过程。针对图片读取不完整的问题,通过循环读取并使用ByteArrayOutputStream进行缓存,确保图片完整下载。同时,修正了文件目录创建逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天老同学发给我一个NIO读取读片的类,看看是什么问题,主要源代码如下

public static byte[] readNIO(String imageUrl) throws IOException {
        byte[] result = new byte[1024000];
        URL url = new URL(imageUrl);
        ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());
        ByteBuffer buffer = ByteBuffer.allocate(1024000);
        readableByteChannel.read(buffer);
        result =  buffer.array();
        return   result;
    }

 public static void main(String[] args) {
        try {
            getFile(readNIO("https://csdnimg.cn/release/edu/resource/images/special/block_chain/block1title.png"),"D:\\","a11.png");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 根据byte数组,生成文件
     */
    public static void getFile(byte[] bfile, String filePath,String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(filePath+"\\"+fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bfile);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

首先看到的问题是创建目录的地方,做了修改,如下

File dir = new File(filePath);
            if (!dir.exists()) {//如果不存在
                dir.mkdirs();
            }
            if (!dir.isDirectory()) {//如果不是目录
                dir.delete();
                dir.mkdirs();
            }
            file = new File(filePath + "\\" + fileName);

然后跑一边代码,发现图片只保存了一半,应该是没有读取完全,所以问题应该是在读取的地方,然后把读取的大部分代码稍微修改下,打印下看看

1b65e701fa984d71aa354e9a0d7864b543b.jpg

可以看到循环读取了两次,所以基本上是没有读取完的情况,所以作如下修改,循环读取并写入ByteArrayOutputStream里面,然后清空缓存,在继续读取,最后再从这里面返回字节数据,这样就不会造成读取不完整的问题了。

public static byte[] readNIO(String imageUrl) throws IOException {
        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        URL url = new URL(imageUrl);
        ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());
        ByteBuffer buffer = ByteBuffer.allocate(1024000);
        int len = 0;
        while ((len = readableByteChannel.read(buffer)) > 0) {
            System.out.println(len);
            byteArray.write(buffer.array(), 0, len);
            buffer.clear();
        }
        return byteArray.toByteArray();
    }

 

转载于:https://my.oschina.net/feiyang2017/blog/3012099

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值