本文共 746 字,大约阅读时间需要 2 分钟。
String id = request.getParameter("downLoadFileId");
String fileName = request.getParameter("fileName");
String path = "C:\\upload\\"+id+"\\"+fileName;(存放文件的路径)
response.setHeader("Content-Disposition", "attachment;filename="+fileName);
response.setHeader("Connection", "close");
response.setHeader("Content-Type", "application/octet-stream");
try{
OutputStream os = response.getOutputStream(); //不加此行将只能下载文本文件.下载jpg等就会出现打不开的现象.
FileInputStream fis = new FileInputStream(path);
byte[] b = new byte[1024];
int i = 0;
while ( (i = fis.read(b)) > 0 )
{
os.write(b, 0, i);
}
fis.close();
os.flush();
os.close();
}
catch ( Exception e )
{
System.out.println ( "IOException." + e );
}
posted on 2006-07-26 15:25 Crespo 阅读(339) 评论(0) 编辑 收藏 所属分类: Java web
转载地址:http://gwnva.baihongyu.com/