欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JSP下载服务器文件的方法

程序员文章站 2024-01-28 14:10:46
本文实例讲述了jsp下载服务器文件的方法。分享给大家供大家参考,具体如下: <%@page import="java.io.fileinputstream"...

本文实例讲述了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程序设计有所帮助。