随笔 - 1, 文章 - 14, 评论 - 3, 引用 - 0
数据加载中……

jsp页面中的下载功能实现

<%

String filename="test.jpg";
String dirName=application.getRealPath("/WEB-INF/upload");
java.io.File ff=null;
String dd=dirName+System.getProperties().getProperty("file.separator")+filename;
try{
ff=new java.io.File(dd);
}
catch(Exception e){
e.printStackTrace();
}
if (ff!=null&&ff.exists()&&ff.isFile())
{
long filelength = ff.length();
InputStream inStream=new FileInputStream(dd);
//设置输出的格式
response.reset();
response.setContentType("application/x-msdownload");
response.setContentLength((int)filelength);
response.addHeader("Content-Disposition","attachment; filename=\"" + toUtf8String(filename) + "\"");
//循环取出流中的数据
byte[] b = new byte[100];
int len;
while((len=inStream.read(b)) >0)
response.getOutputStream().write(b,0,len);
inStream.close();

}
%>

posted on 2006-04-03 10:02 zeroone0 阅读(565) 评论(0)  编辑  收藏 所属分类: 编程


只有注册用户登录后才能发表评论。


网站导航: