首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

jsp action上载

2012-09-14 
jsp action下载private ActionForward filedowndbaction(HttpServletRequest request,HttpServletResponse

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;
}

热点排行