<%@ page contenttype="text/html; charset=gb2312" %>
<%@ page import="java.io.*"%>
<%!
public string toutf8string(string s) {///源于网上
stringbuffer sb = new stringbuffer();
for (int i=0;i<s.length();i++) {
char c = s.charat(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = character.tostring(c).getbytes("utf-8");
} catch (exception ex) {
system.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
sb.append("%" + integer.tohexstring(k).
touppercase());
}
}
}
return sb.tostring();
}
%>
<%
string filename="test.jpg";
string dirname=application.getrealpath("/web-inf/upload");
java.io.file ff=null;
string dd=dirname+system.getproperties().getproperty("file.separator")+filename;
try{
ff=new java.io.file(dd);
}
catch(exception e){
e.printstacktrace();
}
if (ff!=null&&ff.exists()&&ff.isfile())
{
long filelength = ff.length();
inputstream instream=new fileinputstream(dd);
//设置输出的格式
response.reset();
response.setcontenttype("application/x-msdownload");
response.setcontentlength((int)filelength);
response.addheader("content-disposition","attachment; filename=\"" + toutf8string(filename) + "\"");
//循环取出流中的数据
byte[] b = new byte[100];
int len;
while((len=instream.read(b)) >0)
response.getoutputstream().write(b,0,len);
instream.close();
}
%>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器