文件下载

来源:互联网 发布:企业名录搜索软件泄露 编辑:程序博客网 时间:2024/05/17 07:53
 

文件下载

l       Web应用中实现文件下载的两种方式

l       超链接直接指向下载资源

l       程序实现下载需设置两个响应头:

l       设置Content-Type 的值为:application/x-msdownload。Web 服务器需要告诉浏览器其所输出的内容的类型不是普通的文本文件或 HTML 文件,而是一个要保存到本地的下载文件

l       Web 服务器希望浏览器不直接处理相应的实体内容,而是由用户选择将相应的实体内容保存到一个文件中,这需要设置 Content-Disposition 报头。该报头指定了接收程序处理数据内容的方式,在 HTTP 应用中只有 attachment 是标准方式,attachment 表示要求用户干预。在 attachment 后面还可以指定 filename 参数,该参数是服务器建议浏览器将实体内容保存到文件中的文件名称。在设置 Content-Dispostion 之前一定要指定 Content-Type.  

l       因为要下载的文件可以是各种类型的文件,所以要将文件传送给客户端,其相应内容应该被当做二进制来处理,所以应该调用                                方法返回 ServeltOutputStream 对象来向客户端写入文件内容。                       

文件下载操作步骤

ListFileServlet——>listfiles.jsp——>DownloadServlet.java

 

 

1、  ListFileServlet.java

a)    得到文件保存目录的真是路径

b)    创建方法:void listFiles(File file,Map map),迭代处理所有目录及文件,将所有文件存入Map对象中,K——V     uuidname——realname

c)    将Map对象写入request,传至listfiles.jsp

 

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

 

              //得到保存上传文件的文件夹

              String savepath = this.getServletContext().

getRealPath("/WEB-INF/upload");

             

              Map map = new HashMap();

             

              //迭归文件夹下面的所有文件   迭归过程中如何保存迭归出来的数据

              listFiles(new File(savepath),map);

             

              request.setAttribute("map", map);

             

              request.getRequestDispatcher("/listfile.jsp").

forward(request, response);         

       }

 

       private void listFiles(File file,Map map) {

              if(file.isFile()){

                     String uuidname = file.getName(); 

                     String realname = uuidname.

substring(uuidname.indexOf("_")+1);

                     map.put(uuidname, realname);

              }else{

                     //得到目录下所有的文件

                     File files[] = file.listFiles();

                     for(File f: files){

                            listFiles(f,map);

                     }

              }

             

       }

 

 

2、  listfiles.jsp

取出map中的数据,并构建url作为下载链接的href属性值

  <c:forEach var="me" items="${filesMap}">

      <c:url var="fileAddr" value="/servlet/DownloadServlet">

             <c:param name="uuidName">${me.key }</c:param>

             <c:param name="realName">${me.value }</c:param>

      </c:url>

     文件名:${me.value }<a href="${fileAddr }" >下载</a><br/>

   </c:forEach>

显示结果如下

 

 

 

 

3、  DownloadServlet.java

a)    从request中取出文件名等信息,注意中文字符使用的urlencoding,需要进行手动编码转换。

b)    根据文件的uuidName计算出文件的存储路径

c)    判断目标文件是否存在

d)    设置response的header中的content-disposition 值为attachment;filename=realName

e)    获取输入流,并写入输出流

public void doGet(HttpServletRequest request, HttpServletResponse response)

       throws ServletException, IOException {

 

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

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

             

       uuidName = new String(uuidName.getBytes("iso8859-1"),"utf-8");

   //真实文件名取出来必须经过转码,然后因为需要作为下载链接地址,所以需要再一次转码

       realName = URLEncoder.encode(new String(realName.getBytes("iso8859-1"),"utf-8"),"utf-8");

             

       System.out.println(getFileAddr(uuidName));

       File file = new File(getFileAddr(uuidName));

             

       if(!file.exists()){

              request.setAttribute("message", "下载的文件不存在!");

                  request.getRequestDispatcher("/message.jsp")

.forward(request, response);               

       }else{

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

                    

              FileInputStream fin = new FileInputStream(file);                    

              OutputStream out = response.getOutputStream();

                    

              byte[] buffer = new byte[1024];

              int len = 0;

              while((len = fin.read(buffer))>0){

                     out.write(buffer,0,len);

              }

              fin.close();

       }    

}

      

public String getFileAddr(String uuidName){

       int dir1 = uuidName.hashCode() & 0xf;

       int dir2 = (uuidName.hashCode()>>4) & 0xf;

             

String fileAddr = this.getServletContext().getRealPath("/WEB-INF/upload")

+"\\"+ dir1+"\\" + dir2 +"\\" + uuidName;

       return fileAddr;

}