JavaWeb文件下载功能实例代码
在工作中遇到的一个下载文件的功能,自己将其抽取出来,代码简单,希望能帮到大家,好了,话不多说,上代码!
publicvoiddownloadFile(Filefile,StringdownName,HttpServletRequestrequest,HttpServletResponseresponse){
OutputStreamout=null;
FileInputStreamfin=null;
BufferedInputStreambin=null;
try{
if(file.exists()){
StringfinalFileName=null;
Stringagent=request.getHeader("User-Agent");
booleanisMSIE=(agent!=null&&agent.indexOf("MSIE")!=-1);
if(isMSIE){
finalFileName=URLEncoder.encode(downName,"UTF8");
}else{
finalFileName=newString(downName.getBytes("UTF-8"),"ISO-8859-1");
}
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","attachment;filename=".concat(finalFileName));
out=response.getOutputStream();
fin=newFileInputStream(file);
bin=newBufferedInputStream(fin);
for(intdata=bin.read();data>-1;data=bin.read()){
out.write(data);
}
}else{
}
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(bin!=null)
bin.close();
if(fin!=null)
fin.close();
if(out!=null)
out.close();
}catch(Exceptione2){
e2.printStackTrace();
}
}
}
以上就是本文JavaWeb文件下载的代码,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下