<%@ page import="java.io.*,java.util.*;" %>
<%
String downloadFile = "C:\\boot.ini";
BufferedInputStream inputstream = new BufferedInputStream(new
FileInputStream(downloadFile));
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition",
"attachment; filename=\"write.ini\"");
response.setHeader("Accept-ranges", "bytes");
byte[] b = new byte[100];
int len;
OutputStream outp=response.getOutputStream();
try{
while ( (len = inputstream.read(b)) > 0) {
outp.write(b, 0, len);;
}
inputstream.close();
outp.close();}
catch(Exception e){
e.printStackTrace();
outp.close();
}
%>
try{
//取得虚拟的路径
String vpath = CodeConvert.UnicodeToChinese(request.getParameter("path"));
String vname = CodeConvert.UnicodeToChinese(request.getParameter("name"));
String extendName = null;
String realpath = null;
if( vpath.indexOf(":") == -1 ){
//取得真实路径
realpath = getServletConfig().getServletContext().getRealPath("") + vpath;
}
else{
realpath = vpath;
}
extendName = vpath.substring(vpath.lastIndexOf("."));
if( vname == null || vname.equals("") ){
vname = vpath.substring(vpath.lastIndexOf("/")+1);
}else{
vname += extendName;
}
String fn = "attachment; filename="+vname; //必须改为UniCode编码的字符串
System.out.println(fn);
//把标题、内容写到输出流中
response.setHeader("Content-Disposition", new String(fn.getBytes("GB2312"),
"ISO-8859-1"));
createOutput( response.getOutputStream(),realpath);
}catch( Exception ee ){
ee.printStackTrace();
}
%><%!
public void createOutput( OutputStream out,String realpath ) throws IOException {
int b;
BufferedInputStream m_input =
new BufferedInputStream( new FileInputStream(realpath) );
while( (b = m_input.read()) != -1 ){
out.write(b);
}
m_input.close();
out.flush();
out.close();
}
%>现正使用的