加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 教程 > 正文

用Jsp来实现文件下载功能的几种方法

发布时间:2023-05-25 13:18:23 所属栏目:教程 来源:
导读:用Jsp来实现文件下载功能的几种方式

1.最直接最简单的,方式是把文件地址直接放到html页面的一个链接中。这样做的缺点是把文件在服务器上的路径暴露了,并且还无法对文件下载进行其它的控制(如权限)。这个就不
用Jsp来实现文件下载功能的几种方式
 
1.最直接最简单的,方式是把文件地址直接放到html页面的一个链接中。这样做的缺点是把文件在服务器上的路径暴露了,并且还无法对文件下载进行其它的控制(如权限)。这个就不写示例了。
 
2.在服务器端把文件转换成输出流,写入到response,以response把文件带到浏览器,由浏览器来提示用户是否愿意保存文件到本地。(示例如下)
 
<% response.setcontenttype(fileminitype); response.setheader("location",filename); response.setheader("cache-control", "max-age=" + cachetime);     //filename应该是编码后的(utf-8) response.setheader("content-disposition", "attachment; filename=" + filename);   response.setcontentlength(filelength); outputstream outputstream = response.getoutputstream(); inputstream inputstream = new fileinputstream(filepath); byte[] buffer = new byte[1024]; int i = -1; while ((i = inputstream.read(buffer)) != -1) {  outputstream.write(buffer, 0, i);  } outputstream.flush(); outputstream.close(); inputstream.close(); outputstream = null;%>
 
3.既然是jsp的话,还有一种方式就是用applet来实现文件的下载。不过客户首先得信任你的这个applet小程序,由这个程序来接受由servlet发送来的数据流,并写入到本地。
 
servlet端示例
 
public void service(httpservletrequest req, httpservletresponse res)            throws servletexception, ioexception {        res.setcontenttype(" text/plain ");        outputstream outputstream = null;        try {            outputstream = res.getoutputstream();            //把文件路径为srcfile的文件写入outputstream中            popfile(srcfile, outputstream)) ;             } catch (ioexception e) {            e.printstacktrace();         }    }
 
japplet端示例
 
urlconnection con;        try {            //url是被调用的servlet的网址 如 *.do             con = url.openconnection();            con.setusecaches(false);            con.setdoinput(true);            con.setdooutput(true);            con.setrequestproperty("content-type",                "application/octet-stream");            inputstream in = con.getinputstream();            progressmonitorinputstream pminputstream = new progressmonitorinputstream(pane, "正在从服务器下载文件内容", in);            progressmonitor pmonitor = pminputstream.getprogressmonitor();            pmonitor.setmillistodecidetopopup(3);            pmonitor.setmillistopopup(3);            //localfilepath本地路径,localstr文件文件夹,filename本地文件名            string localfilepath = localstr + filename ;            //方法savefilsavefilee是把输入流pminputstream写到文件localfilepath中             if(savefilsavefilee(localfilepath,pminputstream)){            openlocalfile(localfilepath);            }
 
4.顺便把japplet上传文件的代码也贴上来.
 
japplet端示例
 
urlconnection con;        try {            con = url.openconnection();            //url是被调用的servlet的网址 如 *.do                   con.setusecaches(false);            con.setdoinput(true);            con.setdooutput(true);            con.setrequestproperty("content-type","application/octet-stream");              outputstream out = con.getoutputstream();            //localfilepath本地路径,localstr文件文件夹,filename本地文件名            string localfilepath = localstr + filename;            //文件getoutputstream是把文件localfilepath写到输出流out中            getoutputstream(localfilepath,out);            inputstream in = con.getinputstream();            return true;        }catch (ioexception e) {               system.out.println("文件上传出错!");            e.printstacktrace();        }
 
servlet端代码示例
 
public void service(httpservletrequest req, httpservletresponse res)           throws servletexception, ioexception {        res.setcontenttype(" text/plain ");        inputstream inputstream = null;        try {            inputstream = res.getinputstream();//把输入流inputstream保存到文件路径为srcfile的文件中            writefile(srcfile, inputstream);        } catch (ioexception e) {            e.printstacktrace();        }    } // end service
 
总结:在文件的传输中是流的形式存在的,在硬盘上是文件的形式存在的。我们要做的只是通过httpservletrequest和httpservletresponse,或者是response和request来发送流和读取流。以及把文件转换成流或把流转换成文件的操作。
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!