Java實現文件點擊沒反應的方法
更新時間:2018年07月25日 16:34:40 作者:不是那個小明
jsp頁面鏈接,點擊訪問action用IO流去下載服務器上的文件,問題是任憑怎么點擊都沒反應,日志也不報錯。這篇文章給大家介紹Java實現文件點擊沒反應的方法,需要的朋友參考下吧
jsp頁面鏈接,點擊訪問action用IO流去下載服務器上的文件,問題是任憑怎么點擊都沒反應,日志也不報錯。
前臺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);
注:采用這種方式頁面會彈出一個空白窗口,下載之后窗口自動關閉,如果不想顯示這個窗口,使用form提交的方式
總結
以上所述是小編給大家介紹的Java實現文件點擊沒反應的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
使用SpringBoot打jar包并部署到Tomcat詳細步驟
今天帶大家來學習怎么使用SpringBoot打jar包并部署到Tomcat,文中有非常詳細的步驟及代碼示例,對正在學習java的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
Spring實現在非controller中獲取request對象
這篇文章主要介紹了Spring實現在非controller中獲取request對象方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringCloud Webflux過濾器增加header傳遞方式
這篇文章主要介紹了SpringCloud Webflux過濾器增加header傳遞方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02

