1,首先要生成excel文件,==> 

如下代码:

        HSSFWorkbook workbook = new HSSFWorkbook();
                String newPath = DataConvertHelper.getExportPath("mp/egtev/recover/batterydot/");
            String realPath = request.getSession().getServletContext().getRealPath("")+  newPath;
        //1. 判断路径是 否存在,不存在则创建,在目录下创建excel文件
        File dir = new File(realPath);
        if (!dir.isDirectory())
            dir.mkdirs();
        String fileName = realPath + File.separator + "电池回收网点申报" + ".xls";
        File file = new File(fileName);
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                throw new FileNotFoundException("directory " + file.getParent() + " not found");
            } else {
                file.createNewFile();
            }
        }
//转化为流
        OutputStream os = new FileOutputStream(fileName);

//写入流 和 关流

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition",
                "attachment;filename=" + URLEncoder.encode("电池回收网点申报" +".xls", "UTF-8"));// 默认名称车辆表
        
        response.flushBuffer();
    
        workbook.write(os);//写入流
                workbook.close();//关流

//前端要获取的返回地址

        UploadResult uploadResult = new UploadResult();
//获取的地址,为相对地址
        String downloadUrl = newPath.replace(File.separator, "/") + "/" + "电池回收网点申报" + ".xls";
        uploadResult.setDownloadUrl(downloadUrl);
        uploadResult.setResult("1");
        uploadResult.setMsg("操作成功!");

 

//设置 返回

    private void returnHtml(UploadResult uploadResult, HttpServletResponse response) {
        try {
            JSONObject jsonObject = JSONObject.fromObject(uploadResult);
            String jsonStr = jsonObject.toString();
            response.setContentType("application/json;charset=UTF-8");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
//            response.getWriter().write(jsonStr);
//            response.getWriter().flush();
//            response.getWriter().close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

总结: 生成excel 后,获取相对地址的excel信息,返回给前端,前端通过 IP+url的形式 获取相对路径,如:

容器的相对路径的地址,具体方式为:
String newPath = DataConvertHelper.getExportPath("mp/egtev/recover/batterydot/"); 原路径

String realPath = request.getSession().getServletContext().getRealPath("")+  newPath;//具体excel生成的 真实路径,

相对路径为 newPath。