关于文件下载自己忽略的文件中文命名。。。

public void download() throws Exception {
		HttpServletResponse response= ServletActionContext.getResponse();
		HttpServletRequest request =ServletActionContext.getRequest();
		 String path = request.getSession().getServletContext().getRealPath("/")+"excel\\"+"empinport.xls";
		 String fileName="人员批量导入模板.xls";
		 InputStream inStream = new FileInputStream(path);// 文件的存放路径  
		 	response.reset();
	        response.setContentType("bin");
	        response.setHeader( "Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1" ) );  
	        // 循环取出流中的数据
	        byte[] b = new byte[100];
	        int len;
	        try {
	            while ((len = inStream.read(b)) > 0)
	                response.getOutputStream().write(b, 0, len);
	            inStream.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
		}

开始在 ,webroot下excel下为excel命名 为中文, 在本地运行代码,没有一点错误, 当打成war包,上测试的时候却出现找不到文件的错误,困扰。明明没啥错的,当打开war包看路径下的文件时候直接显示一堆乱码,无怪找不到文件。

以后需要记住,这样的问题不要再出现了 。文件路径不能出现中文。 如果要为下载的文件更改名字的话直接 在setheader中更改fileName的名字就可以 了

转载于:https://www.cnblogs.com/wupeng88/p/4561013.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值