博客
关于我
java后台通过http请求下载文件
阅读量:794 次
发布时间:2023-01-28

本文共 1844 字,大约阅读时间需要 6 分钟。

 java后台通过http请求下载文件,直接反馈给前端

public void downLoad(HttpServletResponse response,String HTTP_URL, String filename) {	        BufferedInputStream bis = null;	        BufferedOutputStream bos = null;	        try {	            URL url = new URL(HTTP_URL);	            HttpURLConnection connection = (HttpURLConnection)url.openConnection();	            connection.setRequestMethod("GET");	            connection.setConnectTimeout(5000);	            connection.setReadTimeout(6000);	            connection.connect();	            int responseCode = connection.getResponseCode();	            System.out.println("responseCode=" + responseCode);	            if (responseCode == 200) {	                InputStream is = connection.getInputStream();	                bis = new BufferedInputStream(is);	                response.reset();	                response.setContentType("application/x-msdownload");	                response.setHeader("Content-Disposition", "attachment;filename=".concat(new String(filename.getBytes("GBK"), "ISO-8859-1")));	                OutputStream fos = response.getOutputStream();	                bos = new BufferedOutputStream(fos);	                boolean b = false;	                byte[] byArr = new byte[1024];	                int b1;	                while((b1 = bis.read(byArr)) != -1) {	                    bos.write(byArr, 0, b1);	                }	                bos.flush();	            }	            connection.disconnect();	        } catch (Exception var21) {	            var21.printStackTrace();	        } finally {	            try {	                if (bis != null) {	                    bis.close();	                }	                if (bos != null) {	                    bos.close();	                }	            } catch (IOException var20) {	                var20.printStackTrace();	            }	        }	    }

 

转载地址:http://tsryk.baihongyu.com/

你可能感兴趣的文章
LabVIEW和Web Service交互方式?LabVIE本地项目如何发布到互联网上让外网访问
查看>>
labview如何加载库_LabVIEW中调用DLL文件实现温湿度数据显示(VI源码+驱动库+图片说明)...
查看>>
labview如何把A数组的第一个数据插入到B数组的最后一个元素
查看>>
Lake Counting
查看>>
lambda 与列表理解性能
查看>>
Lambda 实现超强排序
查看>>
Lambda 表达式(使用前提、“类型推断”、作用、优缺点、Lambda还能省略的情况)【java8新特性------Lambda 表达式】
查看>>
lambda表达式
查看>>
lambda表达式与匿名内部类与双冒号(::)
查看>>
Lambda表达式入门,看这篇就够了!
查看>>
Lammp安装过程
查看>>
lamp 一键安装
查看>>
Lamp(Fpm-Php)基本配置
查看>>
LAMP下添加支持openssl的主机
查看>>
LAMP与LNMP架构详解
查看>>
LAMP网站平台搭建
查看>>
LangChain:链接语言与智能的未来
查看>>
LangSmith的简单介绍
查看>>
laradock 安装使用 kafka
查看>>
laravel 5.3 给容器传参
查看>>