@RequestMapping(value=ClientAdminUrl.my_excel_report, method = RequestMethod.GET)
public void getExcelReport(String userId, String shortDates, Model model,
final HttpServletResponse response, final HttpServletRequest request){
String reportFilePath = SiteConfig.getSiteConfigProp(MY_REPORT_PATH);
String userReportPath = reportFilePath + File.separator + userId+ File.separator;
String myExcelReportPath = userReportPath + userId+System.currentTimeMillis() + File.separator;
UserInfo userInfo = userInfoService.selectByPrimaryKey(userId);
String userName = userInfo.getUserName();
String fileName = "report_"+userName+".zip";
String reportZipFile = myExcelReportPath + File.separator+ fileName;
String zipFileRootPath = "my Report";
makeExcelReportPathDirectory(myExcelReportPath);
for(String date: shortDates.split(",")) {
generateExcel(userId, Integer.parseInt(date), myExcelReportPath, userName);
}
try {
CompressUtils.packToolFiles(myExcelReportPath, zipFileRootPath, reportZipFile);
} catch (IOException e) {
LOGGER.error("failed to zip excel.");
}
InputStream is = null;
try {
is = new FileInputStream(reportZipFile);
int contentLength = is.available();
response.setContentType("application/octet-stream");
response.setContentLength(contentLength);
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
now follows javascript block:
function doExport(){
var obj = $('#DetailTbody input[name="checkbox"]');
if(obj.size() == 0){
return;
}
var userId = null;
var shortDates = '';
$.each(obj,function(i) {
if($(this).is(":checked")) {
userId = $(this).val();
if(shortDates == '') {
shortDates += $(this).parents("tr").children().eq(0).text();
}else {
shortDates += ',' + $(this).parents("tr").children().eq(0).text();
}
}
});
if(userId == null || userId == '' || userId == 'undefined'){
$.messager.alert('Message',$.i18n.prop("my.log.export.alert.message"));
return;
}
//start export
window.location.href = contextPath + "/clientAdmin/my/report/excel?userId="+userId + "&shortDates="+shortDates;
}
when I try to export the excel,and debug ,found that if userId contains '#',for example,'user#123',then the java code ,userid came out to be'user',and the words after '#' all removed.and the parameter shortDates is null.
as to the space issue on foxfire.the solve method is below: java code:
private void download(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException { String fileName = "my download test file";
String encoding = request.getParameter("code");
if ((encoding == null) || (encoding == "undefined")) {
encoding = "utf-8";
}
fileName = Utils.urlDecoder(fileName, new String[] { encoding });
System.out.println(fileName);
String[] dots = fileName.split("[.]");
String postfix = dots[(dots.length - 1)];
if (postfix != null) {
if (("xls".equals(postfix)) || ("xlsx".equals(postfix))) {
response.setContentType("application/msexcel");
}
else if (("doc".equals(postfix)) || ("docx".equals(postfix))) {
response.setContentType("application/msword");
}
else if (("zip".equals(postfix)) || ("rar".equals(postfix))) {
response.setContentType("application/zip");
}
else if (("gif".equals(postfix)) || ("jpg".equals(postfix)) || ("png".equals(postfix)) || ("jpeg".equals(postfix))) {
response.setContentType("image/gif");
}
else if ("pdf".equals(postfix)) {
response.setContentType("image/pdf");
}
else
response.setContentType("application/text");
}
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(REAL_PATH + fileName));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
{
bos.write(buff, 0, bytesRead);
}
} catch (Exception localException) {
} finally {
try {
bos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
to solve this problem is only change this line:
response.setHeader("Content-disposition", "attachment;filename=\"" + fileName +"\"");