本文实例讲述了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程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!