response.setCharacterEncoding("GBK");
ServletOutputStream out1 = response.getOutputStream();
int act = Integer.parseInt(request.getParameter("act"));
String filename="";
if(act==1)filename="1.rar";
else if(act==2)filename="2.rar";
else if(act==3)filename="3.rar";
else filename="c.rar";
File file2 = new File("d:\\"+filename);
if (!file2.exists()) {
response.setContentType("application/x-download");
out1.print("file doesn't exist");
return;
}else {
response.setHeader("Content-Disposition",
"attachment; filename=default\"" + "zip" + "\"");
filename=filename.substring(filename.lastIndexOf("\\"));
response.setHeader("Content-Disposition", "attachment; filename=\""
+ filename + "\"");
}
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(file2));
bos = new BufferedOutputStream(out1);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (IOException e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
servlet文件下载
最新推荐文章于 2023-02-07 21:09:21 发布