溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用Java實(shí)現(xiàn)文件點(diǎn)擊沒(méi)反應(yīng)的方法

發(fā)布時(shí)間:2021-04-15 12:26:17 來(lái)源:億速云 閱讀:183 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹使用Java實(shí)現(xiàn)文件點(diǎn)擊沒(méi)反應(yīng)的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

jsp頁(yè)面鏈接,點(diǎn)擊訪問(wèn)action用IO流去下載服務(wù)器上的文件,問(wèn)題是任憑怎么點(diǎn)擊都沒(méi)反應(yīng),日志也不報(bào)錯(cuò)。

前臺(tái)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);
}
});

后臺(tái)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();
    }

解決方法:文件的下載,在前臺(tái)請(qǐng)求的時(shí)候,只能是form表單請(qǐng)求,或者用window.open的方式,最后我采用了window.open的方式

window.open('/yjy/training/TrainingTimeAction.do?method=downLoadAttchById&timeId=' + timeids);

注:采用這種方式頁(yè)面會(huì)彈出一個(gè)空白窗口,下載之后窗口自動(dòng)關(guān)閉,如果不想顯示這個(gè)窗口,使用form提交的方式

以上是“使用Java實(shí)現(xiàn)文件點(diǎn)擊沒(méi)反應(yīng)的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI