首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >将InputSteram转换成文件输出并下载至本地

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

作者头像
java攻城狮
发布2020-10-10 16:35:18
3K0
发布2020-10-10 16:35:18
举报
文章被收录于专栏:个人积累个人积累

场景

调用第三方文件下载接口,通过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());
        }

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 场景
  • 多个文件压缩并批量下载
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档