& lt% @ page import = \" java.io. * \" % & gt& lt%!公共字符串toUtf8String(字符串" />
& lt% @ page import = " java.io. * " % & gt
& lt%!
公共字符串toUtf8String(字符串s)
{
string buffer sb = new string buffer();
for(int I = 0;我& lts.length()。i++) {
char c = s . charat(I);
if(c & gt;= 0 & amp& ampc & lt= 255) {
某人追加(c);
}否則{
byte[]b;
嘗試{
b = Character.toString(c)。getBytes(" utf-8 ");
} catch (Exception ex) {
system . out . println(ex);
b =新字節[0];
}
for(int j = 0;j & ltb .長度;j++) {
int k = b[j];
if(k & lt;0)k+= 256;
sb . append(" % "+integer . tohexstring(k)。
toupper case());
}
}
}
return sb . tostring();
}
% & gt
& lt%
String filename =新字符串(request.getParameter("filename ")。getBytes(" GBK+0 ")," ISO8859-65438 ");
String dirName="D:/ me。doc ";
Java . io . file ff = null;
string DD = dirName+system . get properties()。getProperty(" file . separator ")+filename;
嘗試{
ff = new Java . io . file(DD);
}
捕捉(異常e){
system . out . println(e . getmessage());
e . printstacktrace();
}
如果(ff!= null & amp& ampff . exists()& amp;& ampff.isFile())
{
長文件長度= ff . length();
InputStream inStream = new file InputStream(DD);
//設置輸出格式。
response . reset();
response . setcontenttype(" application/x-msdownload ");
response . setcontentlength((int)file length);
response . add header(" Content-Disposition "," attachmentfilename = \ "+tout F8 string(filename)+" \ " ";
//循環輸出流中的數據。
byte[] b =新字節[100];
int len
while((len = instream . read(b))& gt;0)
response.getOutputStream()。write(b,0,len);
instream . close();
out . clear();
out = pagecontext . push body();
}
% & gt
:& lta href = " d.jsp " & gtaa & lt/a & gt;
上面那個是用流寫的,但是也可以用超鏈接下載。
只需寫下文件的路徑。