jsp 下载文件

      jsp 直接输出二进制文件怎么办呢?

download.jsp
<% @ page language = " java "  pageEncoding = " utf-8 " %>
<% @ page  import = " java.io.* "   %>
<%
try  {
    FileInputStream fin 
=   new  FileInputStream(application.getRealPath( " / " ) + " /readme.zip " );
    response.addHeader(
" Content-Disposition " , " attachment;filename=read.zip " ); 
    
byte [] buf  =   new   byte [ 1024 ];
    
int  readSize  =  fin.read(buf);
    OutputStream os 
=  response.getOutputStream();
    
    
while (readSize  !=   - 1 ) {
        os.write(buf, 
0 , readSize);
        readSize 
=  fin.read(buf);
    }
    os.flush();
    os.close();
    os 
=   null  ;
    response.flushBuffer();
    out.clear();
    out  
=   pageContext.pushBody();
        
catch  (IllegalStateException e) {

}
%>

webapps/test/readme.zip文件可以被下载,可能第一次会输出文字。

你可能感兴趣的:(java,jsp,OS)