主頁 > 知識庫 > JSP下載服務器文件的方法

JSP下載服務器文件的方法

熱門標簽:團購網(wǎng)站 銀行業(yè)務 科大訊飛語音識別系統(tǒng) 阿里云 服務器配置 Linux服務器 電子圍欄 Mysql連接數(shù)設置

本文實例講述了JSP下載服務器文件的方法。分享給大家供大家參考,具體如下:

%@page import="java.io.FileInputStream"%>
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
%@page language="java" contentType="text/html; charset=UTF-8"%>
%@page import="com.ecc.emp.core.Context" %>
%@page import="java.io.File" %>
%@ include file="http://www.xxxx.com/head.jsp"%>
%
  boolean loadFlag = false;
  ServletOutputStream ou = null;
  FileInputStream fis = null;
  try{  
    Context context=(Context)request.getAttribute("context");
    String accountFilePath = (String)context.getDataValue("accountFilePath");
    //String accountFilePath ="E:/ComeBack/Acc_BOS_CM/WebContent/upload/product/1336873410414custormerStatement.txt";
    //String accountFileName = (String)context.getDataValue("accountFileName");
    String displayFileName = (String)context.getDataValue("displayFileName");
    //String displayFileName ="custormerStatement.txt";
    String fullPath =accountFilePath;
    response.reset();
      response.setContentType("charset=UTF-8");
    response.setContentType("text/HTML");
    //response.setContentType("application/vnd.ms-excel");    
    String filename = new String(displayFileName.getBytes("gb2312"), "ISO8859-1");
    response.setHeader("Content-Disposition","attachment; filename=" + filename);
    //解決HTTPS不能下載的問題
    response.setHeader("Cache-Control","public");    
    ou = response.getOutputStream();
    File file = new File(fullPath);
    fis = new FileInputStream(file);    
    try {
      if (fis != null) {
        int filelen = fis.available();
        byte a[] = new byte[filelen];
        fis.read(a);
        ou.write(a);
        ou.flush();
      }      
      loadFlag=true;
    } catch (Exception e) {
      System.out.println("Excel is not exist!");
    }
    out = pageContext.pushBody();
  }catch(Exception e){
    e.printStackTrace();
  }finally{
    try {
      fis.close();
      ou.close();
      ou = null;
      response.flushBuffer();
    } catch (Exception e) {
      // TODO 自動生成 catch 塊
      e.printStackTrace();
    }
  }
%>

希望本文所述對大家JSP程序設計有所幫助。

您可能感興趣的文章:
  • JSP實現(xiàn)遠程文件下載保存到服務器指定目錄中的方法
  • jsp實現(xiàn)從服務器下載xls文件到客戶端的方法
  • jsp實現(xiàn)Servlet文件下載的方法
  • JSP文件下載功能的4種方法
  • JSP response對象實現(xiàn)文件下載的兩種方式
  • jsp實現(xiàn)文件上傳下載的程序示例
  • jsp 文件下載示例代碼
  • jsp文件上傳與下載實例代碼
  • Jsp頁面實現(xiàn)文件上傳下載類代碼
  • 用JSP下載word文件(不會直接用IE打開)
  • JSP實現(xiàn)從不同服務器上下載文件的方法

標簽:蚌埠 衢州 廣元 棗莊 衡水 大理 萍鄉(xiāng) 江蘇

巨人網(wǎng)絡通訊聲明:本文標題《JSP下載服務器文件的方法》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266