文件下载

文件下载后台代码:

public static void download(HttpServletRequest request,HttpServletResponse response,String filePath){
        ServletContext servletContext = request.getSession().getServletContext();
        File file = new File( filePath);
        //设置文件MIME类型 
        response.setContentType( servletContext.getMimeType( file.getName())); 
        //设置Content-Disposition  
        response.setHeader("Content-Disposition", "attachment;fileName="+file .getName()); 
        OutputStream out; 
        //读取目标文件,通过response将目标文件写到客户端 
        try { 
            InputStream in = new FileInputStream( file); 
            out = response.getOutputStream(); 
            int b = 0; 
            while(( b= in.read())!= -1) { 
                out.write( b); 
            } 
            in.close(); 
            out.close(); 
            out.flush(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
     }

html代码:

<input type= "hidden" id ="download-url" value="<c:url value="/application/fileDownload.do"/>" />
<a class= "btn btn_item" id= "export-page" >导出页面 </a>

js代码:

function fileDownload() {
         var $exportFile = $( '#export-page');
         var downloadUrl = $( '#download-url').val();
         var fileName = 'card_information.html';
         var url = downloadUrl + "?fileName=" + fileName;
         window.location. href = url;
     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值