从服务器下载文件到客户端java_jsp实现从服务器下载xls文件到客户端的方法

本文实例讲述了jsp实现从服务器下载xls文件到客户端的方法。分享给大家供大家参考,具体如下:

参考网上的代码写了一个下载xls文件到客户端的jsp页面,只要将服务器的文件地址传给这个jsp页面就可以实现下载文件到客户端了。

/p>

"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

download

response.setcharacterencoding("gb2312");

request.setcharacterencoding("gb2312");

if (request.getparameter("file") != null) {

outputstream os = null;

fileinputstream fis = null;

try {

string file = request.getparameter("file");

if (!(new file(file)).exists()) {

system.out.println("没有文件");

return;

}

system.out.println("文件名为:"+file);

os = response.getoutputstream();

response.setheader("content-disposition", "attachment;filename=" + file);

response.setcontenttype("application/vnd.ms-excel");//此项内容随文件类型而异

byte temp[] = new byte[1000];

fis = new fileinputstream(file);

int n = 0;

while ((n = fis.read(temp)) != -1) {

os.write(temp, 0, n);

}

} catch (exception e) {

out.print("出错");

} finally {

if (os != null)

os.close();

if (fis != null)

fis.close();

}

out.clear();

out = pagecontext.pushbody();

}

%>

冷山sky_snow

/p>

"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

download

response.setcharacterencoding("gb2312");

request.setcharacterencoding("gb2312");

if (request.getparameter("file") != null) {

outputstream os = null;

fileinputstream fis = null;

try {

string file = request.getparameter("file");

if (!(new file(file)).exists()) {

system.out.println("没有文件");

return;

}

system.out.println("文件名为:"+file);

os = response.getoutputstream();

response.setheader("content-disposition", "attachment;filename=" + file);

response.setcontenttype("application/vnd.ms-excel");//此项内容随文件类型而异

byte temp[] = new byte[1000];

fis = new fileinputstream(file);

int n = 0;

while ((n = fis.read(temp)) != -1) {

os.write(temp, 0, n);

}

} catch (exception e) {

out.print("出错");

} finally {

if (os != null)

os.close();

if (fis != null)

fis.close();

}

out.clear();

out = pagecontext.pushbody();

}

%>

冷山sky_snow

2.另外一个修改后的版本(下载文件名可包含中文)

download

response.setcharacterencoding("utf-8");

request.setcharacterencoding("utf-8");

string filepath = new string(request.getparameter("file").getbytes("iso-8859-1"),"utf-8");

system.out.println("============================"+filepath);

if (filepath != null) {

outputstream os = null;

fileinputstream fis = null;

try {

string file = filepath;

if (!(new file(file)).exists()) {

system.out.println("没有文件");

return;

}

string filefilename = file.substring(file.lastindexof("\\")+1);

system.out.println("文件名为:"+filename);

os = response.getoutputstream();

response.setheader("content-disposition", "attachment;filename=" + new string(filename.getbytes("gbk"), "iso-8859-1"));

response.setcontenttype("application/octet-stream");//八进制流 与文件类型无关

byte temp[] = new byte[1024];

fis = new fileinputstream(file);

int n = 0;

while ((n = fis.read(temp)) != -1) {

os.write(temp, 0, n);

}

} catch (exception e) {

out.print("出错了");

} finally {

if (os != null)

os.close();

if (fis != null)

fis.close();

}

out.clear();

out = pagecontext.pushbody();

}

%>

希望本文所述对大家jsp程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值