<%@ 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();
}
%>
