用serlet实现文件下载

原文http//blog_sina_com_cn/s/blog_49a269ae01008yv2.html

之前没怎么用过这个,这次项目中用到了,于是总结一下:

应该说主要注意三方面:

1、写一个servlet

2、在web.xml中配置这个servlet

3、页面上调用这个servlet

第一:

写一个servlet,当然要继承javax.servlet.http.HttpServlet,重写post或者get方法(看需要),这个servlet的思想就是将要下载的文件写到文件流中去,而这个文件输出流就是HttpServletResponse对象的输出流,如下所示:

public class DownloadServlet extends
HttpServlet{
 
 
 public void doPost(HttpServletRequest
request,HttpServletResponse response) throws
ServletException,IOException
 {
  try
  {
   String
downFilename=request.getParameter("curfile");
   String
filepath=request.getParameter("path");
        response.setContentType("text/plain");
        response.setHeader("Location",downFilename);
        response.setHeader("Content-Disposition", "attachment; filename=" +
downFilename);
        OutputStream outputStream = response.getOutputStream();
        InputStream inputStream = new
FileInputStream(filepath+downFilename);
        byte[] buffer = new byte[1024];
        int i = -1;
        while ((i = inputStream.read(buffer)) != -1) {
         outputStream.write(buffer, 0, i);
        }
        outputStream.flush();
        outputStream.close();
  }catch(FileNotFoundException
e1)
  {
   System.out.println("没有找到您要的文件");
  }
  catch(Exception e)
  {
   System.out.println("系统错误,请及时与管理员联系");
  }
    }
 
 
 public void doGet(HttpServletRequest
request,HttpServletResponse response) throws ServletException,
IOException
 {
  doPost(request,response);
 }

}

web.xml配置:

<servlet>
  <servlet-name>download</servlet-name>
  <servlet-class>com.dichain.common.tools.DownloadServlet</servlet-class>
 </servlet>

<servlet-mapping>
  <servlet-name>download</servlet-name>   
  <url-pattern>/Download</url-pattern>
 </servlet-mapping>

jsp中调用servlet弹出一个下载对话框:

window.open
(‘Download?curfile=‘+curfile+‘&path=‘+path,‘newwindow‘,‘height=30,width=40,top=‘+yy+‘,left=‘+xx+‘,toolbar=no,menubar=no,scrollbars=no,
resizable=no,location=no, status=no‘);

时间: 2024-08-25 04:49:45

用serlet实现文件下载的相关文章

PHP实现文件下载

PHP实现文件下载 PHP下载文件 js下载文件 项目操作中不可避免要提供文件的下载,有时候要写一些逻辑判断或者提示啥,那下载的方法就需要做些调整.做个下载文件的集锦: readfile — 输出文件 (手册上有说明:读入一个文件并写入到输出缓冲.) <?php $file = 'monkey.gif' ; if ( file_exists ( $file )) { header ( 'Content-Description: File Transfer' ); header ( 'Conten

struts2文件下载

<!-- 文件下载 --> <action name="download" class="cn.action.demo2.DownloadAction"> <!-- 返回 流结果 --> <result type="stream"> <!-- inputName 用于指定 返回输入流的方法名 默认值 inputStream --> <!-- targetFile 需要在Action

Struts2 多文件下载

Step1:导入支持jar包 commons-fileupload-1.3.1.jar commons-io-2.4.jar jstl-1.2.jar standard-1.1.2.jar commons-compress-1.10.jar 文件压缩工具包 Step2:编写请求下载jsp <script type="text/javascript" src="jquery-1.8.3.js"></script> <%@taglib pr

iOS开发 -文件下载(4 暂停和恢复)

iOS开发网络篇—文件下载(四·暂停和恢复) 一.Range简单说明 通过设置请求头Range可以指定每次从网路下载数据包的大小 Range示例 bytes=0-499 从0到499的头500个字节 bytes=500-999 从500到999的第二个500字节 bytes=500- 从500字节以后的所有字节 bytes=-500 最后500个字节 bytes=500-599,800-899 同时指定几个范围 Range小结 - 用于分隔 前面的数字表示起始字节数 后面的数组表示截止字节数,没

[WebApi] 捣鼓一个资源管理器--文件下载

<打造一个网站或者其他网络应用的文件管理接口(WebApi)第一章--之-- "文件下载" > ======================================================== 作者:qiujuer 博客:blog.csdn.net/qiujuer 网站:www.qiujuer.net 开源库:Genius-Android 转载请注明出处:blog.csdn.net/qiujuer/article/details/41621781 =====

文件下载Demo

知识点: //获取用户要下载的资源的名称        string name=context.Request.Params["downloadName"];        //设置响应报文中,当前资源是一个附件,需要下载.        context.Response.AddHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(name));    

android webview downloadManager文件下载管理

一.downloadmanager类说明: 从Android 2.3开始新增了一个下载管理类,在SDK的文档中我们查找android.app.DownloadManager可以看到.下载管理类可以长期处理多个HTTP下载任务,客户端只需要给出请求的Uri和存放目标文件的位置即可,下载管理使用了一个AIDL服务器所以可以放心的在后台执行,同时实例化的方法需要使用getSystemService(Context.DOWNLOAD_SERVICE) ,Android123再次提醒使用API Level

PHP + NGINX 控制视频文件播放,并防止文件下载

最简单的方法是使用NGINX的 internal 功能 server {    listen 80;    server_name www.xxx.com;     location / {        index index.php index.html index.htm;        root  /xxx; if (!-e $request_filename) {         rewrite ^/index.php(.*)$ /index.php?s=$1 last;      

java文件下载

文件下载,可以是post请求,也可以是get请求. 新建web项目,在WebRoot下建up目录存放上传的文件: 最简单的但是实际不会这样做的下载方式,直接用a标签指向文件目录,就能下载: <a href="<c:url value='/up/a.jpg'/>">下载jpg</a> <br /> <a href="<c:url value='/up/cos.jar'/>">下载jar</a