没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
java通过通过url下载文件并输出的方法下载文件并输出的方法
下面小编就为大家分享一篇java通过url下载文件并输出的方法,具有很好的参考价值,希望对大家有所帮助。
一起跟随小编过来看看吧
实例如下所示:实例如下所示:
controller:
@RequestMapping(value = "/{loanid}/{atmtType}")
public void doGet(@PathVariable("loanid") String loanid,@PathVariable("atmtType") String atmtType,
HttpServletRequest req,HttpServletResponse response) {
Map<String,String> map = new HashMap<String,String>();
map.put("loanid", loanid);
Map<String, String> urlByLoanid= new HashMap<String,String>();
// Map<String, String> urlByLoanid = zcmQueryInfoService.queryUrlByLoanid(map);
try {
if(urlByLoanid!=null){
String wjurl="http://10.0.15.11:8080/gateway//nfs/marvel-core-admin/2017/10/11/compact_seal_17101119371231615_7.pdf";
//String wjurl = urlByLoanid.get("url");
int i = wjurl.lastIndexOf("/");
String fileName = wjurl.substring(i+1);
URL url = new URL(wjurl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
InputStream inputStream = conn.getInputStream();
//获取自己数组
byte[] bs = readInputStream(inputStream);
response.setContentType("application/octet-stream;charset=ISO8859-1");
BufferedOutputStream output = null;
BufferedInputStream input = null;
try {
output = new BufferedOutputStream(response.getOutputStream());
// 中文文件名必须转码为 ISO8859-1,否则为乱码
String fileNameDown = new String(fileName.getBytes(), "ISO8859-1");
// 作为附件下载
response.setHeader("Content-Disposition", "attachment;filename=" + fileNameDown);
output.write(bs);
response.flushBuffer();
} catch (Exception e) {
log.error("Download log file error", e);
} // 用户可能取消了下载
finally {
if (input != null)
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
if (output != null)
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 从输入流中获取字节数组
* @param inputStream
* @return
* @throws IOException
*/
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
资源评论
weixin_38670529
- 粉丝: 3
- 资源: 928
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- STM32单片机FPGA毕设电路原理论文报告一种基于单片机的节能断电保护电路设计
- STM32单片机FPGA毕设电路原理论文报告一种基于单片机的机床位移检测系统
- unity3D翻书插件
- STM32单片机FPGA毕设电路原理论文报告一种基于单片机的红外测控系统
- Date(1).cpp
- java基于ssm珠宝商城购物网站案例设计
- STM32单片机FPGA毕设电路原理论文报告一种基于单片机的灯光调光控制系统开发
- STM32单片机FPGA毕设电路原理论文报告一种基于单片机的超声测距系统的设计
- Visual Basic快速入门.md
- STM32单片机FPGA毕设电路原理论文报告一种基于单片机的半导体激光器电源控制系统的设计
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功