预览加载中,请您耐心等待几秒...
1/6
2/6
3/6
4/6
5/6
6/6

在线预览结束,喜欢就下载吧,查找使用更方便

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

jsp如何实现下载文件的功能 第一步:创建Servlet 第二步:写代码‍ publicclassFielDownLoadextendsHttpServlet{ /***Constructoroftheobject.*/publicFielDownLoad(){super();} /***Destructionoftheservlet.<br>*/publicvoiddestroy(){super.destroy();//Justputs"destroy"stringinlog//Putyourcodehere} /***ThedoGetmethodoftheservlet.<br>**Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.**@paramrequesttherequestsendbytheclienttotheserver*@paramresponsetheresponsesendbytheservertotheclient*@throwsServletExceptionifanerroroccurred*@throwsIOExceptionifanerroroccurred*/publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ doPost(request,response);} /***ThedoPostmethodoftheservlet.<br>**Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.**@paramrequesttherequestsendbytheclienttotheserver*@paramresponsetheresponsesendbytheservertotheclient*@throwsServletExceptionifanerroroccurred*@throwsIOExceptionifanerroroccurred*/publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ Stringfilename=request.getParameter("file_name");if(filename==null)filename="";filename=filename.trim(); InputStreaminStream=null;Stringattchname=""; byte[]b=newbyte[100];intlen=0;try{attchname=getAttachName(filename);//取得附件的名称filename=getRealName(request,filename);//取得附件的全路径if(filename==null){response.setContentType("text/html;charset=GBK");response.getWriter().print("<spanstyle='color:red'>文件不存在,或者禁止下载!</span>");return;}attchname=toUtf8String(attchname);//将文件转码UTF-8inStream=newFileInputStream(filename);response.reset();//必须reset,否则会出现文件不完整SmartUploadsu=newSmartUpload();//新建一个SmartUpload对象su.initialize(this.getServletConfig(),request,response);//初始化//设定contentDisposition为null以禁止浏览器自动打开文件,//保证点击链接后是下载文件。若不设定,则下载的文件扩展名为//doc时,浏览器将自动用word打开它。扩展名为pdf时,//浏览器将用acrobat打开。su.setContentDisposition(null);su.downloadFile(filename);//下载文件//循环取出流中的数据while((len=inStream.read(b))>0){response.getOutputStream().write(b,0,len);}inStream.close();