java excel模板 下载_springboot下载excel模板

在网上找了半天,获取springboot resource下面的文件,各种实验,最终解决了,废话不说直接上代码

/**

* 描述:下载外部案件导入模板

* @throws exception

*/

@requestmapping(value = "/downloadexcel")

@responsebody

public void downloadexcel(httpservletresponse res, httpservletrequest req,string name) throws exception {

string filename = name+".xlsx";

servletoutputstream out;

res.setcontenttype("multipart/form-data");

res.setcharacterencoding("utf-8");

res.setcontenttype("text/html");

string filepath = getclass().getresource("/template/" + filename).getpath();

string useragent = req.getheader("user-agent");

if (useragent.contains("msie") || useragent.contains("trident")) {

filename = java.net.urlencoder.encode(filename, "utf-8");

} else {

// 非ie浏览器的处理:

filename = new string((filename).getbytes("utf-8"), "iso-8859-1");

}

filepath = urldecoder.decode(filepath, "utf-8");

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

fileinputstream inputstream = new fileinputstream(filepath);

out = res.getoutputstream();

int b = 0;

byte[] buffer = new byte[1024];

while ((b = inputstream.read(buffer)) != -1) {

// 4.写到输出流(out)中

out.write(buffer, 0, b);

}

inputstream.close();

if (out != null) {

out.flush();

out.close();

}

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值