欢迎光临
我们一直在努力

jsp页面中的下载功能实现-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

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

}

%>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » jsp页面中的下载功能实现-JSP教程,Jsp/Servlet
分享到: 更多 (0)

相关推荐

  • 暂无文章