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

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

优化方案一:原始流复制

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

Word-As-Image for Semantic Typography Word-As-Image for Semantic Typography

文字变形艺术字、文字变形象形字

Word-As-Image for Semantic Typography 62 查看详情 Word-As-Image for Semantic Typography

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的转换速度?的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/947799.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月1日 17:10:36
下一篇 2025年12月1日 17:10:57

相关推荐

发表回复

登录后才能评论
关注微信