关于一个支持多线程下载的Servlet

类别:Java 点击:0 评论:0 推荐:

Servelet:

package com.syit.scmis.common;

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @version  1.0
 * @author
 */
public class GetFile
 extends HttpServlet
 implements Servlet {

 /**
 * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
 public void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
  doPost(req,resp);
 }

 /**
 * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
 public void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
  req.setCharacterEncoding("GBK");
  String spath=req.getParameter("path");
  if (spath.equals("")){
   java.io.PrintWriter out=resp.getWriter();
   out.println("Current directory is invalidate!");
  }
  else{
   if (spath.indexOf("/download")==0){
    req.setAttribute("parent",spath.substring(1));
    spath=getServletContext().getRealPath(spath);
    DownloadFile f=new DownloadFile();
    if (f.getFileEx(spath,req,resp)==1){
     req.getRequestDispatcher("/download.jsp").forward(req,resp);
    }
   }else{
    java.io.PrintWriter out=resp.getWriter();
    out.println("Current directory is invalidate!");
   }
  }
 }

}

主要的工作Java类:
/*
 * 创建日期 2004-10-13
 *
 */
package com.syit.scmis.common;

import java.io.*;
import java.util.*;
import javax.servlet.http.*;

/**
 *
 * @author Fuqiming
 * @version 1.0
 *
 */
public class DownloadFile {
 
 public int getFileEx(String strpath,HttpServletRequest req,HttpServletResponse resp) throws IOException{
  File f=new File(strpath);
  if (f.isDirectory()){
   req.setAttribute("list",getFile(strpath));
   return 1;
  }
  else if (f.isFile()){
   downFile(strpath,req,resp);
   return 0;
  }else {
   throw new IOException("path is invalidate");
  }
 }
 
 public ArrayList getFile(String strpath){
  ArrayList array=new ArrayList();
  File f=new File(strpath);
  File[] fs=f.listFiles();
  for (int i=0;i<fs.length;i++){
   array.add(fs[i]);
  }
  return array;
 }
 
 public void downFile(String s,HttpServletRequest req,HttpServletResponse resp) throws IOException{
  if (s==null){
   throw new IOException("File name is must nt null");
  }
  File file =new File(s);
  if (!file.isFile()){
   throw new IOException("File is not exist");
  }
  System.out.println(req.getRemoteAddr()+"请求下载:"+s);
  long l=file.length();
  long p=0;
  resp.reset();
  resp.setHeader("Accept-Ranges", "bytes");
  if (req.getHeader("Range") != null)
  {
   resp.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);
   String sclient=req.getHeader("Range");
   sclient=sclient.substring("bytes=".length());
   if (sclient.charAt(sclient.length()-1)=='-') sclient=sclient.substring(0,sclient.length()-1);
   p = Long.parseLong(sclient);
  }
  resp.setHeader("Content-Length", new Long(l - p).toString());
  if (p != 0)
  {
   resp.setHeader("Content-Range","bytes " + new Long(p).toString() + "-" + new Long(l -1).toString() + "/" + new Long(l).toString());
  }
  resp.setContentType("application/octet-stream");
  resp.setHeader("Content-Disposition",
   "attachment;filename="+new String(file.getName().getBytes(),"iso-8859-1"));
  long k =0;
  int ibuffer=65536;
  byte[] bytes=new byte[ibuffer];
  FileInputStream fileinputstream = new FileInputStream(file);
  try{
   if (p!=0) fileinputstream.skip(p);
   while (k<l){
    int j=fileinputstream.read(bytes,0,ibuffer);
    resp.getOutputStream().write(bytes,0,j);
    resp.getOutputStream().flush();
    k +=j;
   }
   System.out.println(req.getRemoteAddr()+"请求下载:"+s+"下载完毕");
  }
  catch(Exception e){
   System.err.println("传输"+s+"失败,位置:"+k);
   System.err.println(e.getMessage());
  }
  finally{
   fileinputstream.close();
  }
 }
}

本文地址:http://com.8s8s.com/it/it14328.htm