模板下载

Posted acme6

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模板下载相关的知识,希望对你有一定的参考价值。

function downloadExcelTemplate_Click(){
     var url = "downloadExcelTemplate.do?isdebug=true";
     location.href = url;
}

  

/**
     * 下载导入模板
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/downloadExcelTemplate.do", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ResponseModel downloadExcelTemplate(HttpServletRequest request, HttpServletResponse response) {
        ResponseModel responseModel = new ResponseModel();
        responseModel.setStatusCode("fail");
        String header = request.getHeader("User-Agent").toUpperCase();
        String fileName = "z中文";
        try {
            if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
                fileName = URLEncoder.encode(fileName, "utf-8");
                fileName = fileName.replace("+", "%20");    //IE下载文件名空格变+号问题
            }
        
            String filepath = request.getSession().getServletContext().getRealPath("/") +"excel/excel_route.xlsx";
            InputStream is = new FileInputStream(filepath);
            OutputStream os = setRespone(fileName, response);
            int length = 1024;
            int readLength = 0;
            byte buf[] = new byte[1024];
            readLength = is.read(buf, 0, length);
            while (readLength != -1) {
                os.write(buf, 0, readLength);
                readLength = is.read(buf, 0, length);
            }
            os.flush();
            os.close();
            responseModel.setStatusCode("success");
        } catch (UnsupportedEncodingException e) {
            getLogger().error("AccountInputController.downloadExcelTemplate method UnsupportedEncodingException  :" + e.getMessage(), e);
        } catch (FileNotFoundException e) {
            getLogger().error("AccountInputController.downloadExcelTemplate method FileNotFoundException  :" + e.getMessage(), e);
        } catch (Exception e) {
            getLogger().error("AccountInputController.downloadExcelTemplate method Exception :" + e.getMessage(), e);
        }
        return responseModel;
    }
/**
     * 设置输出响应下载流
     *
     * @param fileName
     * @param response
     * @return
     * @throws IOException
     */
    private OutputStream setRespone(String fileName, HttpServletResponse response) throws IOException {
        //输出流
        OutputStream os = response.getOutputStream();
        //重置输出流
        response.reset();

        response.setContentType("application/vnd.ms-excel");
        //设置响应标题>这里浏览器会提示用户选择下载文件需要存放的路径>
        //后续生成的文件在输出流关闭后>action返回detailExcel进result指定响应内容为excel>自动写入该excel到该用户指定的路径中
        response.setHeader("Content-disposition", "attachment; fileName=" + new String((fileName + ".xlsx").getBytes(), "iso8859-1"));
        //response.setHeader("Content-disposition", "attachment; fileName=" + URLEncoder.encode(brow,"utf-8")+".xls");(不可用)
        return os;
    }

 

以上是关于模板下载的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段(vue主模板)

VSCode自定义代码片段2——.vue文件的模板

Eclipse 中的通用代码片段或模板

调用模板化成员函数:帮助我理解另一个 *** 帖子中的代码片段

vs 2010代码片段

vs 2010代码片段