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

    @RequestMapping( value = "/download.shtml", method = { RequestMethod.GET, RequestMethod.POST } )
    public void downloadFile( HttpServletResponse response )
    {
     File file  = new File( wjlj );
     String fileName = file.getName();
     response.setCharacterEncoding( "utf-8" ); //设置编码格式
     response.setContentType( "multipart/form-data" );         //设置文件ContentType类型,这样设置,会自动判断下载文件类型  
     response.setHeader( "Content-Disposition", "attachment;fileName=" + fileName );     // 2.设置文件头:最后一个参数是设置下载文件名  

    //(这里  可以设置成excel格式 :response.setHeader("Content-Disposition", "attachment;fileName=" + “文件名” + ".xsl");
            //可以设置成.pdf格式 :response.setHeader("Content-Disposition", "attachment;fileName=" + “文件名” + ".pdf");
     InputStream inputStream = null;
     try {
      inputStream = new FileInputStream( file );
      OutputStream os = response.getOutputStream();
      byte[] b = new byte[2048];
      int length;
      while ( (length = inputStream.read( b ) ) > 0 )
      {
       os.write( b, 0, length );
      }
      os.close();
     } catch ( Exception e ) {
      e.printStackTrace();
     } finally {
      if ( inputStream != null )
      {
       try {
        inputStream.close();
       } catch ( IOException e ) {
        e.printStackTrace();
       }
      }
     }
    }

  • 相关阅读:
    phpcms列表页内容如何替换?
    如何用phpcms将静态网页生成动态网页?
    简单介绍phpcms以及phpcms如何安装?
    注册账号的验证
    js跳转页面
    流程审核处理
    关于XML(可扩展标记语言)的基础知识与写法
    php随机获取验证码
    流程管理
    HDU 5894 hannnnah_j’s Biological Test
  • 原文地址:https://www.cnblogs.com/libo199374/p/8862512.html
Copyright © 2011-2022 走看看