zoukankan      html  css  js  c++  java
  • 下载文件

    String filename="wxyh.xls";
            File file=new File(this.getRequest().getRealPath("/")+filename);
            byte[] contents=new byte[(int) file.length()];
            FileInputStream fis=new FileInputStream(file);
            fis.read(contents);
            downloadFile(filename,contents);
            if(fis!=null){
                fis.close();
            }
    
    
    
    public void downloadFile(String fileName, byte[] bytes) throws Exception {
            
           
            String fileNameTemp = URLEncoder.encode(fileName, "UTF-8");
            
            HttpServletResponse response = getResponse();
            response.reset();
            response.setContentType("application/x-download"); // windows
            response.addHeader("Content-Disposition", "attachment;filename="
                    + fileNameTemp);
    
            OutputStream output = response.getOutputStream();
            output.write(bytes);
            output.flush();
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                output = null;
            }
        }

    **

    有问题在公众号【清汤袭人】找我,时常冒出各种傻问题,然一通百通,其乐无穷,一起探讨


  • 相关阅读:
    【NOIP2017】奶酪
    【NOIP2017】时间复杂度
    【NOIP2005】过河
    【洛谷习题】垃圾陷阱
    dfs序
    bzoj2441 小W的问题
    彩色迷宫
    蛋糕与蛋挞
    树上倍增
    因数个数定理
  • 原文地址:https://www.cnblogs.com/qingmaple/p/4164980.html
Copyright © 2011-2022 走看看