解决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();
        }
    }

 

posted on 2016-08-16 17:21  little fat  阅读(1779)  评论(0编辑  收藏  举报