// 按组联系导出联系人(把文件先通过poi上传到服务器的目录,在去服务器下载到(另存为的目录))
public ActionForward exportByGroup(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
// Excel 文件存放在服务器的相对路径下
String outputFile = request.getRealPath("/tmp");
File folder = new File(outputFile);
folder.mkdirs();
System.out.println("导出时服务器端文件保存的位置为:" + outputFile);
HttpSession session = request.getSession();
LinkmanForm linkmanForm = (LinkmanForm) form;
Page page = linkmanForm.getPage();
List<Linkman> list = null;
String file = "";
try {
Integer pid = (Integer) session.getAttribute("groupid");
String groupid = pid.toString();
list = null;
if (pid == 0 || "".equals(groupid)) {
page = manService.queryNull(page);
list=manService.queryNullAll();
} else {
page = manService.queryByGroup(groupid,page);
list=manService.queryByGroupAll(groupid);
}
file = LinkMan_excel.getPayExcel(list, outputFile);
} catch (Exception e) {
e.printStackTrace();
}
file = file.replace("\\", "/");
//转码,重点(防止文件名乱码)
String fileName = URLEncoder.encode(file.substring(file
.lastIndexOf("/") + 1, file.length()), "UTF-8");
ServletOutputStream sos = response.getOutputStream();
FileInputStream fis = null;
try {
//重点
response.reset();
response.setHeader("content-disposition", "attachment;filename="
+ fileName);
// response.setContentType("application/vnd.ms-excel");//此项内容随文件类型而异
response.setContentType("APPLICATION/OCTET-STREAM");
byte temp[] = new byte[1000];
fis = new FileInputStream(file);
int n = 0;
while ((n = fis.read(temp)) != -1) {
sos.write(temp, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
}
fis.close();
sos.flush();
sos.close();
request.setAttribute("page", page);
return mapping.findForward("list.view");
}