/**
* 下载pdf
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward downnewYearPdf(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
byte[] buffer;
InputStream ins;
String dirPaths, filepath;
try {
// 工程类路径,用来获取保存文件的的路径
String url = request.getSession().getServletContext().getRealPath(request.getRequestURI());
int a = url.lastIndexOf(File.separator);
url = url.substring(0, a);
String name = "newyear.pdf";//文件名字
filepath = "/newyear/newYear.pdf";//文件根目录下的路径
dirPaths = url + filepath;//总路径
try {
// 读取文件
if (!"".equals(dirPaths)) {
ins = new FileInputStream(dirPaths);
int bytesRead = 0;
buffer = new byte[4096];
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment;"
+ " filename="
+ new String(name.getBytes(), "ISO-8859-1"));
while ((bytesRead = ins.read(buffer, 0, 4096)) != -1) {
try {
response.getOutputStream().write(buffer);
response.getOutputStream().flush();
} catch (Exception ex) {
throw new Exception("读数据流出现错误");
}
}
buffer = null;
response.getOutputStream();
ins.close();
}
} catch (Exception ex) {
request.setAttribute(Constants.C_ERROR_MSG, "“" + name
+ "文件不存在硬盘中“");
return mapping.findForward("error");
}
} catch (Exception ex) {
request.setAttribute(Constants.C_ERROR_MSG, "文件下载出现错误");
return mapping.findForward("error");
}
return null;
}