zoukankan      html  css  js  c++  java
  • 解决jsp下载文件,迅雷下载路径不显示文件名称的问题

    如果浏览器安装了迅雷的插件,在jsp页面调用java后台实现文件下载功能时,会自动弹出迅雷下载,迅雷的下载路径会显示.do或者.xhtml之类的,为了解决这个问题,jsp页面修改如下:

    写一个<a>标签在页面上:

    <a id="downloadUrl" target="_blank"></a>

    导出按钮如下:

    <a href="javascript:void(0)" id="biz_exp_button" class="easyui-linkbutton"
        data-options="iconCls : 'icon-download',plain:true" onclick="javascript:DataMonitor.file_download('wbj');">导出</a>

    js代码如下:

    var DataMonitor = {
        file_download : function(param)
            {
            url = CONTEXT_PATH + "/sjjk/file_download/";
            var $a = $("#downloadUrl");
            if(param == 'wbj'){
                url += encodeURI("委办局接入数据") + ".xls";
                $a.attr("href", url)[0].click();
            }else if(param == 'csjcj'){
                url += encodeURI("城市级基础库数据") + ".xls";
                $a.attr("href", url)[0].click();
            }else {
                
            }
            $a.attr("href", null);
        }
    };    

    java代码如下:

    @RequestMapping("file_download/{fileName}.xls")
        public void fileDownload(@PathVariable String fileName,
                HttpServletResponse response, HttpServletRequest request)
                throws Exception {
            // String type = request.getParameter("type");
            String type = null;
            String filename = "";
            if (null != fileName) {
                filename = fileName.concat(".xls");
                if ("委办局接入数据".equals(fileName)) {
                    type = "wbj";
                } else if ("城市级基础库数据".equals(fileName)) {
                    type = "csjcj";
                }
            }
            if (null == type) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
            String userAgent = request.getHeader("User-Agent");
            if (userAgent.contains("MSIE") || userAgent.contains("Trident")
                    || userAgent.contains("Edge")) {
                filename = java.net.URLEncoder.encode(filename, "UTF-8");
            } else {
                // 非IE浏览器的处理:
                filename = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
            }
            String realPath = request.getSession().getServletContext()
                    .getRealPath("");
            String filePath = this.dataMonitorService
                    .queryDataToExp(type, realPath);//生成xls文件,返回路径,根据自己的业务修改
            File file = new File(filePath);
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;fileName="
                    + filename);
            response.setContentLength((int) file.length());
            ServletOutputStream out;
            try {
                FileInputStream inputStream = new FileInputStream(file);
                out = response.getOutputStream();
                int b = 0;
                byte[] buffer = new byte[512];
                while (b != -1) {
                    b = inputStream.read(buffer);
                    out.write(buffer, 0, b);
                }
                inputStream.close();
                out.close();
                out.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
  • 相关阅读:
    浅谈ConcurrentHashMap实现原理
    HashMap底层实现原理及扩容机制
    浅谈fail-fast机制
    《从Lucene到Elasticsearch:全文检索实战》学习笔记五
    《从Lucene到Elasticsearch:全文检索实战》学习笔记四
    JVM垃圾回收算法解析
    《从Lucene到Elasticsearch:全文检索实战》学习笔记三
    《从Lucene到Elasticsearch:全文检索实战》学习笔记二
    python print()内置函数
    《从Lucene到Elasticsearch:全文检索实战》学习笔记一
  • 原文地址:https://www.cnblogs.com/coprince/p/5777151.html
Copyright © 2011-2022 走看看