添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
备案 控制台
学习
实践
活动
专区
工具
TVP
写文章
专栏首页 个人积累 将InputSteram转换成文件输出并下载至本地
3 1

海报分享

将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();