日期:2014-05-16  浏览次数:20346 次

jsp action下载
private ActionForward filedowndbaction(HttpServletRequest request,
HttpServletResponse response, String path, String filename)
throws Exception {
try {
response.setContentType("APPLICATION/OCTET-STREAM");
String filedownload = path + "\\" + filename;
String filedisplay = filename;
filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename="
+ filedisplay);
OutputStream outp = null;
FileInputStream in = null;
try {
outp = response.getOutputStream();
in = new FileInputStream(filedownload);
byte[] b = new byte[1024];
int i = 0;
while ((i = in.read(b)) > 0) {
outp.write(b, 0, i);
}
outp.flush();
if (in != null) {
in.close();
in = null;
}
if (outp != null) {
outp.close();
outp = null;
}

File fl = new File(filedownload);

fl.delete(); //删除源文件
} catch (Exception e) {
System.out.println("Error!");
e.printStackTrace();
} finally {

}
} catch (IOException e) {
e.printStackTrace();
request.setAttribute("msg", "文件下载失败");
}
return null;
}