首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

没应用UploadFile实现文件下载Demo源码

2013-08-16 
没使用UploadFile实现文件下载Demo源码1.首先本小姐很久没写代码,很多东西其实都忘的差不多,特别是Html的

没使用UploadFile实现文件下载Demo源码
1.首先本小姐很久没写代码,很多东西其实都忘的差不多,特别是Html的一些标签和js更是忘得一塌糊涂,由于这次我们公司要做个上传下载的web服务器,改服务器是跟android客户端提供数据的,也就是说这个ftpweb下载服务器主要是android端提供文件下载等服务的,但是我用jsp页面做了个demo,原理差不多,只是跟android端传递的不是下载文件数据,而是包含下载路径的下载文件信息,以下是我写的小demo;




2.下面是Jsp页面代码:
<script type="text/javascript">
           
          
            function  xiaozhukuaipaoOnclick(){
           
            var filename  =  "小猪快跑13.01.apk";
            document.getElementById("form").action="xiazai?fileName="+encodeURI(encodeURI(filename));
            document.getElementById("form").submit();
           
            }
           
            function  xiaoyazhiOnclick(){
           
            var filename  = "小鸭子.APK" ;
            document.getElementById("form").action="xiazai?fileName="+encodeURI(encodeURI(filename));
            document.getElementById("form").submit();
           // document.forms[0].action  + =("&fileName="+filename) ;
           // document.forms[0].submit();
            }
           
            function GoLiveOnclick(){
            var filename  ="GoLive TV.11.02.apk";
            // alert(document.forms[0].action);
           // document.forms[0].action+("&fileName="+filename) ;
            //document.forms[0].action="xiazai?fileName="+filename" ;
           // document.forms[0].submit(); 
           document.getElementById("form").action="xiazai?fileName="+encodeURI(encodeURI(filename));
           document.getElementById("form").submit();
            }
           
  </script>
  
   <form   id="form" enctype="multipart/form-data"  method="post">
   <li><span>小猪快跑游戏.APK </span> 
   <input  type="button"  value="下载"  name="xiaozhukuaipao"  id="xiaozhukuaipao"  onclick="xiaozhukuaipaoOnclick()"> </li>
 
   <li><span>小鸭子.APK </span>       
   <input  type="button"  value="下载"  name="xiaoyazhi"  id="xiaoyazhi"     onclick="xiaoyazhiOnclick()"> </li>
   
  <li><span> GoLive TV.11.02.apk </span>       
   <input  type="button"  value="下载"  name="GoLive"  id="GoLive"     onclick="GoLiveOnclick()"> </li>
   
   </form>




3.下面是我的servlet类:



package download.control;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;



import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class DownLoadControlServlet extends  HttpServlet  {

private static final long  serialVersionUID = 1l;
private  DownLoadControlServlet  downLoadControlServlet  ;

  

public void init() throws ServletException{


      System.out.println("DownLoadControlServlet   init()?????..");
}

public void init(ServletConfig config) throws ServletException{
   super.init(config);
 
       System.out.println("DownLoadControlServlet Config()");
}
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException{
    System.out.println(req+"---"+resp);
  this.doPost(req, resp)    ;     
      System.out.println("DownLoadControlServlet doGet()");
}

public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException{
  System.out.println(req+"---"+resp);
  HttpServletResponse   response = resp;
      HttpServletRequest   request = req;
  
      System.out.println("DownLoadControlServlet doGet()");
    
     // String   fileName =  req.getParameter("fileName");
    
     // PrintWriter  printWriter = new  PrintWriter() ;
      /*if(fileName == null || fileName == ""){
     
      System.out.print("用户请求下载的文件不存在");
     
     
      }
      */
      //将文件流写入浏览器

      OutputStream outputStream = resp.getOutputStream();
     
      ServletContext servletContext=req.getSession().getServletContext();
   
      String fileName  =req.getParameter("fileName")  ;
      File file = new File(servletContext.getRealPath("/WEB-INF/downLoad/"), fileName);
    //  File  file =new File("D:/download/res/" , "fileName");
      System.out.println(file.getPath()+"-------");
      resp.setHeader("Content-disposition","attachment; filename="+fileName); 
     resp.setContentType("application/vnd.ms-excel;charset=GBK");
     resp.setBufferSize(1024);
     resp.setHeader("Cache-Control","no-cache"); //HTTP 1.1
     resp.setHeader("Pragma","no-cache"); //HTTP 1.0
      resp.setDateHeader ("Expires", 0); //prevents caching at the proxy server
     
     
      if (file.exists()) {
       
        FileInputStream fileInputStream = new FileInputStream(file);
      
        byte[] b = new byte[1024];
        int length;
        while ((length = fileInputStream.read(b)) != -1) {
            outputStream.write(b, 0, length);
            outputStream.flush();
        }
    }
    else {
        System.out.println("下载的文件不存在"); 
    }
   
  
}
public void destroy(){

System.out.println("Servlet destroy()....");

}




}




4.从代码中可以看出我的下载文件是放在webInfo下面的downLoad文件下面,


5.在web.xml配置servlet应该都会,但是还是写上吧


<servlet>
<servlet-name>DownLoadControlServlet</servlet-name>
<servlet-class>download.control.DownLoadControlServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DownLoadControlServlet</servlet-name>
<url-pattern>/xiazai</url-pattern>
</servlet-mapping>


热点排行