0
@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 +"\"");

daxue
  • 259
  • 1
  • 2
  • 11

1 Answers1

1

In a URL, the # character is used at the beginning of the hash part. Everything following this character is "client only", and not sent to the server.

You have to quote this character, replace # by %23 in your javascript code.

You can use the javascript function encodeURIComponent :

window.location.href = contextPath +
  "/clientAdmin/my/report/excel?userId=" + encodeURIComponent(userId) +
  "&shortDates=" + encodeURIComponent(shortDates);
Jérémie B
  • 10,611
  • 1
  • 26
  • 43
  • thx!Is this your meaning?if userId contains a '#',I can replace it with %23 first,then put it to the url.but whatif there is another user whose id cantains '%23'?sorry,I am not English-speaking people,a little hard for me to show all what i mean... – daxue Feb 17 '16 at 01:24
  • i have updated my answer to show you how to quote a url in javascript – Jérémie B Feb 17 '16 at 07:04
  • it is very helpful.the **#** issue resoled,but is still has problems when it contains a **space**.as i code this for exporting a excel, and the **space** char has already been encoded by '%20',but when I try to export the excel ,it can only get a file-----an unkonw type file,this issue only show on foxfire,but chorme is OK. – daxue Feb 18 '16 at 02:47
  • I have solve this problem.and updated on my question text.thx – daxue Feb 18 '16 at 03:22
  • sorry cannot vote you now , for i am new here.when i get the qualification,will do it. – daxue Feb 18 '16 at 10:03