首页 > Java > java教程 > 正文

5MB图片加载耗时8秒,如何优化BufferedInputStream的转换速度?

花韻仙語
发布: 2024-12-02 09:30:04
原创
823人浏览过

5mb图片加载耗时8秒,如何优化bufferedinputstream的转换速度?

bufferedinputstream转换速度优化

对于下述代码,当图片大小为5mb时,加载时间耗时8秒。如何提升其加载速度?

url url = new url(imageurl);
httpurlconnection connection = (httpurlconnection) url.openconnection();
connection.setrequestmethod("get");
connection.setconnecttimeout(5000);
bufferedinputstream bis = new bufferedinputstream(connection.getinputstream());
bytearrayoutputstream baos = new bytearrayoutputstream();
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1) {
    baos.write(buffer, 0, len);
}
outputstream outputstream = response.getoutputstream();
response.setcontenttype("image/jpg");
outputstream.write(baos.tobytearray());
outputstream.flush();
outputstream.close();
登录后复制

优化方案

该代码存在以下优化点:

  • 内存占用高:读取数据后全部存放在内存中,当文件较大时会造成内存溢出。
  • 阻塞读取:阻塞读取数据后才执行写入,造成资源浪费。
  • 连接未复用:未复用http连接,增加开销。
  • 资源未释放:未释放http连接和response流,造成内存泄露。

优化方案一:原始流复制

优化原始流复制,通过增大缓冲区,提高效率:

httpurlconnection connection = null;
try {
    url url = new url(imageurl);
    connection = (httpurlconnection) url.openconnection();
    connection.setrequestmethod("get");
    connection.setconnecttimeout(5000);
    try (inputstream bis = new bufferedinputstream(connection.getinputstream());
         outputstream out = response.getoutputstream()) {
        response.setcontenttype("image/jpg");
        
        // buffer 越大,效率越快
        byte[] buffer = new byte[1024];
        int len;
        while ((len = bis.read(buffer)) != -1) {
            out.write(buffer, 0, len);
            
            out.flush();
            
        }
    }
} catch (exception e) {
    throw new runtimeexception(e);
} finally {
    if(null != connection){
        connection.disconnect();
    }
}
登录后复制

优化方案二:使用复制工具

使用第三方库提供的方法复制流,减少代码复杂度:

httpurlconnection connection = null;
try {
    url url = new url(imageurl);
    connection = (httpurlconnection) url.openconnection();
    connection.setrequestmethod("get");
    connection.setconnecttimeout(5000);
    try (inputstream bis = new bufferedinputstream(connection.getinputstream());
         outputstream out = response.getoutputstream()) {
        response.setcontenttype("image/jpg");

        ioutil.copy(bis, out);
        
    }
} catch (exception e) {
    throw new runtimeexception(e);
} finally {
    if(null != connection){
        connection.disconnect();
    }
}
登录后复制

优化方案三:使用nio非阻塞传输

使用nio非阻塞传输,减少系统开销,提升效率:

HttpURLConnection connection = null;
try {
    URL url = new URL(imageUrl);
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setConnectTimeout(5000);
    try (ReadableByteChannel in = Channels.newChannel(new BufferedInputStream(connection.getInputStream()));
         WritableByteChannel out = Channels.newChannel(response.getOutputStream())) {
        response.setContentType("image/jpg");

        ByteBuffer byteBuffer = ByteBuffer.allocate(8192);
        while (in.read(byteBuffer) != -1) {
            // 写转读
            byteBuffer.flip();
            out.write(byteBuffer);
            byteBuffer.clear();
        }
    }
} catch (Exception e) {
    throw new RuntimeException(e);
} finally {
    if (null != connection) {
        connection.disconnect();
    }
}
登录后复制

以上就是5MB图片加载耗时8秒,如何优化BufferedInputStream的转换速度?的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号