专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

将InputSteram转换成文件输出并下载至本地

场景

调用第三方文件下载接口,通过HttpClient的方式进行调用,需要从HttpResponse解析出参数,并读取流变成文件下载 调用部分

/**
*   调用GET请求  文件下载
*
*/
public static void fileDownload(String url,String cookie) throws Exception {
    HttpClient client = null;
    HttpGet get = new HttpGet(url);
    get.setHeader("Content-Type", "application/x-www-form-urlencoded");
    get.setHeader("Cookie", cookie);
    try {
        RequestConfig.Builder customReqConf = RequestConfig.custom();
        customReqConf.setConnectTimeout(DEFAULT_CONNECTION_TIME_OUT.intValue());
        customReqConf.setSocketTimeout(DEFAULT_CONNECTION_TIME_OUT.intValue());
        get.setConfig(customReqConf.build());
        HttpResponse res = null;
        client = createSSLInsecureClient();
        res = client.execute(get);
        HttpEntity entity = res.getEntity();
        InputStream inputStream = entity.getContent();

        //这里的流文件亦可以直接转换成 MutiParFile文件 ,
        ////MultipartFile multipartFile = new MockMultipartFile("temp.jpg","temp.jpg","", inputStream);

        String rootPath ="C:\\Users\\Administrator\\Desktop\\";

        String suffix = ".png";
        Long index = System.currentTimeMillis();

        String fileName = rootPath + index + suffix;
        writeToLocal(fileName,inputStream);

    } finally {
        get.releaseConnection();
        if ((url.startsWith("https")) && (client != null) && ((client instanceof CloseableHttpClient))) {
            ((CloseableHttpClient) client).close();
        }
    }
}

    /**
     * 文件下载
     * @param destination  下载路径
     * @param input
     * @throws IOException
     */
public static void writeToLocal(String destination, InputStream input)
            throws IOException {
    int index;
    byte[] bytes = new byte[1024];
    FileOutputStream downloadFile = new FileOutputStream(destination);
    while ((index = input.read(bytes)) != -1) {
        downloadFile.write(bytes, 0, index);
        downloadFile.flush();
    }
    input.close();
    downloadFile.close();

    }

多个文件压缩并批量下载

private void getZip(List<String> files,HttpServletResponse response){

        response.setContentType("application/x-msdownload");
        response.setHeader("Content-Disposition","attachment;filename=人脸图像.zip");

        String rootPath = configuration.getRootpath();
        if(CollectionUtils.isEmpty(files)){

            log.error(rootPath + "路径不存");
        }
        String zipName ="人脸图像.zip";
        String zipPath = rootPath + zipName;
        BufferedInputStream bis =null;
        try {
            //ZipOutputStream zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipPath)));
            ZipOutputStream zipOutput = new ZipOutputStream(response.getOutputStream());
            for(String str :  files) {
                File  file = new File(rootPath ,str);
                if(!file.exists()){
                    log.error("文件被删除");
                    continue;
                }
                ZipEntry zEntry = new ZipEntry(file.getName());
                zipOutput.putNextEntry(zEntry);
                bis = new BufferedInputStream(new FileInputStream(file));
                byte[] buffer = new byte[1024];
                int read = 0;
                while((read = bis.read(buffer)) != -1){
                    zipOutput.write(buffer, 0, read);
                }
            }
            zipOutput.finish();
            bis.close();
            zipOutput.close();

        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

}

文章永久链接:https://tech.souyunku.com/40810

未经允许不得转载:搜云库技术团队 » 将InputSteram转换成文件输出并下载至本地

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们