码迷,mamicode.com
首页 > 编程语言 > 详细

java文件下载

时间:2018-08-14 14:23:16      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:context   int   content   block   trace   NPU   read   ade   leo   

LocalServletContext localContext = (LocalServletContext) context;

//1.获取到响应对象
HttpServletResponse response = localContext.getResponse();

String FilePath= (String) context.getData("Url");

if(FilePath != null && !"".equals(FilePath)){
File file=new File(FilePath);
if(!file.exists()){
try {
response.sendError(404, "File not found !");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
}
try {
BufferedInputStream br =new BufferedInputStream(new FileInputStream(file));
byte [] buf=new byte[1024];
int len=0;
response.reset();
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment;filename="+file.getName());

OutputStream output=new FileOutputStream(file);// 通过对象多态性,进行实例化
while((len=br.read(buf))>0){
output.write(buf, 0, len);
}
output.flush();
br.close();
output.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

java文件下载

标签:context   int   content   block   trace   NPU   read   ade   leo   

原文地址:https://www.cnblogs.com/mayay/p/9473907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!