zoukankan      html  css  js  c++  java
  • 一个使用Servlet文件实现文件下载的实例

    一个使用Servlet文件实现文件下载的实例

    (可以扩充本实例实现:对用户隐藏他要下载文件的路径,或者在下载文件时要做一些其他的工作,如检查用户有没有下载此文件的权限等)

    了解在Servlet中如何控制输出流以及response对象的contentType相关知识

    一个Servlet文件DownloadFile,在此Servlet中读取要下载的文件,然后写到响应流中以达到用户下载文件的目的。要下载的文件可以放在任何地方,并且是对用户隐藏的。

    在DownloadFile Servlet中,首先要得到要下载文件的文件名filename,同时要预先定义好文件保存的路径,然后设置response对象的内容类型和头信息,最后读取要下载文件的字节流并写到response的输出流中。

    DownloadFile.java源文件

    package myservlet;

    import java.io.*;

    import javax.servlet.*;

    import javax.servlet.http.*;

    public class DownloadFile extends HttpServlet {

    //字符编码

    private final String ENCODING="GB2312";

    //内容类型

    private final String CONTENT_TYPE="text/html;charset=gb2312";

    //要下载的文件存放的路径

    private String downloadfiledir="d:\temp\";

    public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{

    //设置request对象的字符编码

    request.setCharacterEncoding(ENCODING);

    //从request中取出要下载文件的名字

    String filename=request.getParameter("filename");

    if(filename==null||filename.trim().equals("")){

    //设置response对象的ContentType

    response.setContentType(CONTENT_TYPE);

    //输出错误信息

    PrintWriter out=response.getWriter();

    out.println("<font color=red>输入的文件名无效!</font>");

    out.close();

    }else{

    //下载文件的完整路径名

    String fullfilename=downloadfiledir+filename;

    System.out.println("下载文件:"+fullfilename);

    //根据文件的类型设置response对象的ContentType

    String contentType=getServletContext().getMimeType(fullfilename);

    if(contentType==null)

    contentType="application/octet-stream";

    response.setContentType(contentType);

    //设置response的头信息

    response.setHeader("Content-disposition","attachment;filename=""+filename+""");

    InputStream is=null;

    OutputStream os=null;

    try{

    is=new BufferedInputStream(new FileInputStream(fullfilename));

    //定义输出字节流

    ByteArrayOutputStream baos=new ByteArrayOutputStream();

    //定义response的输出流

    os=new BufferedOutputStream(response.getOutputStream());

    //定义buffer

    byte[] buffer=new byte[4*1024]; //4k Buffer

    int read=0;

    //从文件中读入数据并写到输出字节流中

    while((read=is.read(buffer))!=-1){

    baos.write(buffer,0,read);

    }

    //将输出字节流写到response的输出流中

    os.write(baos.toByteArray());

    }catch(IOException e){

    e.printStackTrace();

    }finally{

    //关闭输出字节流和response输出流

    os.close();

    is.close();

    }

    }

    }

    public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{

    //调用doGet()方法

    doGet(request,response);

    }

    }
  • 相关阅读:
    IOS客户端Coding项目记录(二)
    IOS客户端Coding项目记录(一)
    IOS开发基础知识--碎片7
    图解域域树域林根域的含义
    Windows server 2012公用网络修改为专用网络
    Windows2012R2版本区别
    VMWare:vSphere6 企业版参考序列号
    Oracle快速测试连接是否成功
    Brocade300 commands
    也谈免拆机破解中兴B860av1.1(解决不能安装软件/解决遥控)
  • 原文地址:https://www.cnblogs.com/huapox/p/3509883.html
Copyright © 2011-2022 走看看