文件下载和实现案例

文件下载和实现案例

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

         超链接直接指向下载资源

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

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

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

 

 

下载案例

遍历上传目录下的所有文件显示给用户,并允许用户完成下载

         列出所有的下载资源

ListFileServlet-----listfiles.jsp-----DownloadServlet.java

ListFileServlet:读取某一个文件夹下所有的文件---list集合对象----存入request作用域范围内

Listfile.jsp:将所有文件列表显示

DownloadServlet.java:

<%@ page language="java"import="java.util.*" pageEncoding="utf-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core"prefix="c" %>

<%

String path =request.getContextPath();

String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>list files</title>

   

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet"type="text/css" href="styles.css">

    -->

 

  </head>

 

  <body>

    <h2>下载资源列表</h2>

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

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

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

    </c:url>

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

    </c:forEach>

  </body>

</html>

 

 

 

 

package com.hbsi.servlet;

 

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.HashMap;

import java.util.Map;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

 

public class ListFileServlet extendsHttpServlet {

         publicListFileServlet() {

                   super();

         }

         publicvoid destroy() {

                   super.destroy();// Just puts "destroy" string in log

                   //Put your code here

         }

         //将某一个文件夹下的文件获取到存入到Map uuidname--File对象

         publicvoid doGet(HttpServletRequest request, HttpServletResponse response)

                            throwsServletException, IOException {

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

                   StringsavePath=this.getServletContext().getRealPath("WEB-INF/upload");

                   //建hashMap对象

                   Mapmap=new HashMap();

                  

                   listFiles(newFile(savePath),map);

                   //存到作用域

                   request.setAttribute("map",map);

                   //转向

                   request.getRequestDispatcher("/listfiles.jsp").forward(request,response);

         }

         //递归方法去遍历该文件夹下的所有文件及子文件夹下的文件

         privatevoid listFiles(File file,Map map){

                   //File[]files=file.listFiles()

                   //files[0]files[1]

                   if(file.isFile()){

                            //如果是文件

                            Stringuuidname=file.getName();

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

                            map.put(uuidname,realname);

                   }else{

                            //不是文件而是文件夹

                            File[]files=file.listFiles();

                            for(Filef:files){

                                     listFiles(f,map);

                            }

                   }

         }

         publicvoid doPost(HttpServletRequest request, HttpServletResponse response)

                            throwsServletException, IOException {

 

                   doGet(request,response);

         }

         publicvoid init() throws ServletException {

                   //Put your code here

         }

 

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值