文件下载

362 阅读1分钟

手写代码,加强记忆。

InputStream in = null;

OutputStream out = null;

try{

//读文件

String path = "C://a//b//c.xls";

in = new FileInputStream(path);

//设置输出流

out = response.getOutputStream();

//写文件

int len = 0;

byte[] b = new byte[1024];

while( len != -1){

       len = in.read(b);

       if(len != -1){

            out.write(b, 0, len);

           }

}

}catch(Exception e){

} finally{

     //关闭流(先开后关)

     if(out != null){

              try{

              out.flush();

              out.close();

                 }catch(){

                    }

                     }


                 if(in != null){

try{

in.close();

}catch(){

}

}

}