首页 > 其他分享 >SpringBoot 下载文件

SpringBoot 下载文件

时间:2022-10-09 13:56:30浏览次数:67  
标签:bytesum 文件 fs SpringBoot int new logger 下载 String

下载文件  SpringBoot 接口输出文件流 & Vue 下载文件流,获取 Header 中的文件名

@SpringBootTest
class DownloadTests {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @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();
        }
        new CountDownLatch(1).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);
        }
        return result;
    }
}

 

标签:bytesum,文件,fs,SpringBoot,int,new,logger,下载,String
From: https://www.cnblogs.com/vipsoft/p/16771864.html

相关文章

  • springboot中对各个层的理解
    springboot中对各个层的理解1、entity层:实体层,数据库在项目中的类。也被称为model层,pojo层。用于定义与数据库对象的属性,提供get/set方法,带参和无参的构造方法。一般数......
  • 搭建nginx下载服务器
    1,获取nginx的安装包​​ http://nginx.org/download/nginx-1.9.0.tar.gz​​2,解压tar xvf nginx-1.9.0.tar.gz3,配置nginx的安装目录./configure--prefix=/home/work/......
  • maven篇4:pom文件详解
    1、pom.xml文件详解<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://m......
  • springboot整合ssm详细讲解
    SSM是企业中广泛应用的框架。大家再熟练地使用SSM进行业务逻辑开发的时候,也被它大量的xml配置困扰。今天快速优雅的使用SpringBoot实现简易的SSM工程。废话不多说,comeon开......
  • Springboot整合缓存
    一、缓存的引入一个应用主要瓶颈在于数据库的IO,大家都知道内存的速度是远远快于硬盘的速度(即使固态硬盘与内容也无法比拟)。应用之中经常会遇到返回相同的数据(数据字典,行政......
  • wireshark网络封包抓包工具导入/导出pcap文件
    1、Wireshark导入文件打开Wiresharkwiki,点击SampleCaptures,可以看到Wireshark官方上传的一些pcap文件。点击SampleCaptures后,可以看到文件后缀名有cap,pcap,pcapng,pc......
  • Springboot创建项目(idea版本)
    一:概述由于springboot项目,不管是java工程还是web工程都可以直接以jar方式运行,所以推荐创建jar工程,这里创建jar工程项目为例。二:两种方式创建springboot项目1.第一种方式手动......
  • Springboot自定义Stater
    1、默认启动器Boot会将项目中常用的场景做成对应的starter启动器,项目中涉及到什么场景就引入该场景对应的启动器,项目中引入这些启动器之后,和这个starter相关的依赖也会被引......
  • Spring读取配置文件
    一、Resource在Java程序中,我们经常会读取配置文件、资源文件等。使用Spring容器时,我们也可以把“文件”注入进来,方便程序读取。Spring提供了一个​​org.springframework.co......
  • Springboot自动配置原理
    一个boot项目启动类有个@SpringBootApplication注解,查看此注解主要包括@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan三个注解@SpringBootConfigura......