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


    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的名字就可以 了

  • 相关阅读:
    上下界网络流——概念解析与快速入门(待修改)
    maomao的现在与未来
    exgcd证明和最基础应用
    快速入门Splay
    luogu 2515
    bzoj 1996
    *51nod 1409
    51nod 1412
    51nod 1503
    51nod 1020
  • 原文地址:https://www.cnblogs.com/wupeng88/p/4561013.html
Copyright © 2020-2023  润新知