下载服务器上的文件

本文提供了一个使用Java实现的导出Excel文件的示例代码,详细展示了如何通过设置响应头来解决中文乱码问题,并使用FileInputStream读取本地Excel模板,最后通过ServletOutputStream将文件输出到客户端。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@RequestMapping("downLoadExcel")
public String downLoadExcel(HttpServletRequest request,HttpServletResponse response){
try {
//路径
String path = request.getSession().getServletContext().getRealPath("/template/");
     //下面的两行设置响应头,必须设置,以弹框的形式下载 中文乱码记得要转码啊!!!!!!!!!!!!!
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName="+new String("指数模板.xlsx".getBytes(),"ISO8859-1"));//
ServletOutputStream out;
     //File.separator 文件夹的分割线,不同的操作系统之间可以通用
File file = new File(path+File.separator+ "指数模板.xlsx");
FileInputStream inputStream = new FileInputStream(file);
out = response.getOutputStream();
int len= 0;
byte[] buffer = new byte[1024];
while ((len=inputStream.read(buffer))!=-1){
out.write(buffer,0,len);
}
inputStream.close();
out.close();
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

转载于:https://www.cnblogs.com/feixian/p/5960210.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值