/**
* 下载附件
* @param mapping
* @param form
* @param request
* @param response
* @throws Exception
*/
public void download(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String filename = request.getParameter("filename");
String filepath = request.getParameter("filepath");
log.info("一开始的filepath:"+filepath);
String realPath = request.getSession().getServletContext()
.getRealPath("").replace("\\", "/");
BufferedInputStream bis = null;
ServletOutputStream sos = null;
try{
File file = new File(filepath);
if(!file.exists()){
response.getWriter().write("<script language=javascript>alert('file does not exist!');history.back();</script>");
}else{
response.reset();
String sloadName=new String(filename.getBytes("gb2312"),"iso8859-1");
if (request.getHeader("User-Agent").toLowerCase()
.indexOf("firefox") > 0) {
sloadName = new String(filename.getBytes("gb2312"), "ISO8859-1"); // firefox浏览器
log.info("firefox浏览器 ");
} else if (request.getHeader("User-Agent").toUpperCase()
.indexOf("CHROME") > 0) {
log.info("谷歌浏览器");
System.out.println("谷歌浏览器");
sloadName = new String(filename.getBytes("gb2312"), "ISO8859-1");// 谷歌
} else if (request.getHeader("User-Agent").toUpperCase()
.indexOf("MSIE") > 0) {
sloadName = URLEncoder.encode(filename, "UTF-8");// IE浏览器
}
System.out.println(sloadName);
response.setContentType("bin");
response.setHeader("Content-Disposition","attachment;filename=\""+sloadName+"\"");
bis = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[1024];
int len = 0;
sos = response.getOutputStream();
while ((len = bis.read(buf)) > 0){
sos.write(buf, 0, len);
}
sos.flush();
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(bis!=null)
bis.close();
if(sos!=null)
sos.close();
}
}