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文件可以被下载,可能第一次会输出文字。
posted on 2007-10-16 23:57
流浪汗 阅读(572)
评论(0) 编辑 收藏 所属分类:
JAVA/J2EE