SpringBoot 下载文件
下载文件 SpringBoot 接口输出文件流 & Vue 下载文件流,获取 Header 中的文件名
@SpringBootTest class DownloadTests { Logger logger = LoggerFactory.getLogger(this.getClass()); CountDownLatch countDownLatch; @Test void downloadApi() throws InterruptedException { for (int i = 1; i < 10; i++) { int idx = i; new Thread(() -> logger.info(download("http://www.vipsoft.com.cn/api/download", idx + ".zip"))).start(); } countDownLatch = new CountDownLatch(count); countDownLatch.await(); } public String download(String downloadUrl, String saveName) { String result = ""; // 下载网络文件 int bytesum = 0; int byteread = 0; FileOutputStream fs = null; InputStream inStream = null; try { logger.info("下载地址 => {}", downloadUrl); URL url = new URL(downloadUrl); URLConnection conn = url.openConnection(); inStream = conn.getInputStream(); String savePath = StrUtil.format("{}/{}", "D:\\temp", saveName); fs = new FileOutputStream(savePath); byte[] buffer = new byte[1204]; int length; while ((byteread = inStream.read(buffer)) != -1) { fs.write(buffer, 0, byteread); bytesum += byteread; if (bytesum % (1024 * 1024) < 1000) { System.out.println(Thread.currentThread().getName() + " " + saveName + " 已写入(M) " + (bytesum / 1024 / 1024)); } } fs.flush(); result = StrUtil.format("{}/{}", "D:\\temp", saveName); } catch (FileNotFoundException e) { logger.error(e.getMessage(), e); } catch (IOException e) { logger.error(e.getMessage(), e); } finally { IoUtil.close(inStream); IoUtil.close(fs); countDownLatch.countDown(); } return result; } }
本文来自博客园,作者:VipSoft 转载请注明原文链接:https://www.cnblogs.com/vipsoft/p/16771864.html
分类:
JAVA
标签:
SpringBoot
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
2021-10-09 Spring Boot Admin 配置应用
2021-10-09 SpringBoot Admin OFFLINE
2021-10-09 Spring Boot Admin 离线实例
2016-10-09 Intellij idea 乱码问题(英文操作系统)
2014-10-09 TRUNC 截取日期或数字,返回指定的值。
2012-10-09 根据条件用一个表的字段,去更新另一个表的字段