Java實(shí)現(xiàn)文件點(diǎn)擊沒反應(yīng)的方法
jsp頁面鏈接,點(diǎn)擊訪問action用IO流去下載服務(wù)器上的文件,問題是任憑怎么點(diǎn)擊都沒反應(yīng),日志也不報錯。
前臺ajax代碼
Ext.Ajax.request({ url : '/yjy/training/TrainingTimeAction.do?method=downLoadAttchById', params : { timeId : timeids }, success : function(response,options){ var result = Ext.util.JSON.decode(response.responseText); Ext.Msg.alert("下載成功"); }, failure :function(response,options){ var result = Ext.util.JSON.decode(response.responseText); Ext.Msg.alert("下載失敗"+result.message); } });
后臺action代碼
String timeId = request.getParameter("timeId"); String sql = "select doc_name from CPER.EHRTRAIN_item_DOCUMENT where item_id = ?"; DbHelper dbHelper = new DbHelper(); Object[] params = new Object[]{timeId}; String fileName = (String)dbHelper.runSQLScalar(sql, params); String filePath = ServerPathUtil.getPathRoot()+"WEB-INF/cache/train_item_file/train_item_file_"+timeId+"/"+fileName; File file = new File(filePath); if(!file.exists()){ logger.debug("文件不存在"); throw new IOException("the file not exists"); } response.setContentLength((int) file.length()); OutputStream o = response.getOutputStream(); byte b[] = new byte[5000]; //response.setContentType("application/x-msdownload"); response.setContentType("application/vnd.ms-excel"); response.setContentLength((int)file.length()); response.setHeader("Content-Disposition","attachment; filename="+fileName); FileInputStream in = new FileInputStream(file); int n; while ((n = in.read(b)) != -1) { o.write(b, 0, n); } in.close(); }catch(Exception e){ e.printStackTrace(); }
解決方法:文件的下載,在前臺請求的時候,只能是form表單請求,或者用window.open的方式,最后我采用了window.open的方式
window.open('/yjy/training/TrainingTimeAction.do?method=downLoadAttchById&timeId=' + timeids);
注:采用這種方式頁面會彈出一個空白窗口,下載之后窗口自動關(guān)閉,如果不想顯示這個窗口,使用form提交的方式
總結(jié)
以上所述是小編給大家介紹的Java實(shí)現(xiàn)文件點(diǎn)擊沒反應(yīng)的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
jmeter+ant+jenkins自動化測試環(huán)境配置搭建過程
在搭建jmeter+ant+jenkins環(huán)境有些前提條件,那就是要先配置好java環(huán)境、安裝好jenkins以及配置好jmeter,這樣才能省去很多的事情,對jmeter+ant+jenkins自動化測試環(huán)境配置搭建過程感興趣的朋友一起看看吧2021-12-12java構(gòu)造器的重載實(shí)現(xiàn)實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于java構(gòu)造器的重載實(shí)現(xiàn)實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-01-01Java發(fā)送郵箱驗(yàn)證碼、session校驗(yàn)功能
本篇主要描述“發(fā)送郵箱驗(yàn)證碼、session校驗(yàn)”相關(guān)前(html\js)后(java)臺代碼,業(yè)務(wù)邏輯示例,需要的朋友可以參考下2018-02-02SpringMVC中的handlerMappings對象用法
這篇文章主要介紹了SpringMVC中的handlerMappings對象用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09MybatisPlus整合Flowable出現(xiàn)的坑及解決
這篇文章主要介紹了MybatisPlus整合Flowable出現(xiàn)的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Java里得到00:00:00格式的時分秒的Timestamp
Java里如何得到00:00:00格式的時分秒的Timestamp ,下面是具體的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2009-09-09