博客
关于我
java后台通过http请求下载文件
阅读量:809 次
发布时间: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/

你可能感兴趣的文章
mysql interval显示条件值_MySQL INTERVAL关键字可以使用哪些不同的单位值?
查看>>
Mysql join原理
查看>>
MySQL Join算法与调优白皮书(二)
查看>>
Mysql order by与limit混用陷阱
查看>>
Mysql order by与limit混用陷阱
查看>>
mysql order by多个字段排序
查看>>
MySQL Order By实现原理分析和Filesort优化
查看>>
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql skip-grant-tables_MySQL root用户忘记密码怎么办?修改密码方法:skip-grant-tables
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
MYSQL sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>