0

0

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

花韻仙語

花韻仙語

发布时间:2024-12-02 09:30:04

|

885人浏览过

|

来源于php中文网

原创

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流,造成内存泄露。

优化方案一:原始流复制

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

Article Forge
Article Forge

行业文案AI写作软件,可自动为特定主题或行业生成内容

下载
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();
    }
}

相关专题

更多
http500解决方法
http500解决方法

http500解决方法有检查服务器日志、检查代码错误、检查服务器配置、检查文件和目录权限、检查资源不足、更新软件版本、重启服务器或寻求专业帮助等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

282

2023.11.09

http请求415错误怎么解决
http请求415错误怎么解决

解决方法:1、检查请求头中的Content-Type;2、检查请求体中的数据格式;3、使用适当的编码格式;4、使用适当的请求方法;5、检查服务器端的支持情况。更多http请求415错误怎么解决的相关内容,可以阅读下面的文章。

388

2023.11.14

HTTP 503错误解决方法
HTTP 503错误解决方法

HTTP 503错误表示服务器暂时无法处理请求。想了解更多http错误代码的相关内容,可以阅读本专题下面的文章。

1209

2024.03.12

http与https有哪些区别
http与https有哪些区别

http与https的区别:1、协议安全性;2、连接方式;3、证书管理;4、连接状态;5、端口号;6、资源消耗;7、兼容性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

1761

2024.08.16

php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

138

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

80

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

82

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

61

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

458

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
CSS3 教程
CSS3 教程

共18课时 | 4.2万人学习

Git 教程
Git 教程

共21课时 | 2.4万人学习

麻省理工大佬Python课程
麻省理工大佬Python课程

共34课时 | 5万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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