首页 > 开发 > 综合 > 正文

采用HttpServlet 实现web文件下载

2024-07-21 02:14:49
字体:
来源:转载
供稿:网友
  • 网站运营seo文章大全
  • 提供全面的站长运营经验及seo技术!
  • import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import com.topwisdom.framework.util.*;

    public class webdownload extends httpservlet {
        public webdownload() {
     }

     private servletconfig config;

     public void init(servletconfig config) throws servletexception {
      super.init(config);
      this.config = config;
     }

     public void dopost(httpservletrequest req,httpservletresponse res) throws servletexception {
      doget(req,res);
     }
     file://取得附件的名称
     public static string getattachname(string file_name) {
      if(file_name==null) return "";
      file_name = file_name.trim();
      int ipos = 0;
      ipos = file_name.lastindexof("//");
      if(ipos>-1){
       file_name = file_name.substring(ipos+1);
      }
      ipos = file_name.lastindexof("/");
      if(ipos>-1){
       file_name = file_name.substring(ipos+1);
      }
      ipos = file_name.lastindexof(file.separator);
      if(ipos>-1){
       file_name = file_name.substring(ipos+1);
      }
      return file_name;
     }
     file://utf8转码
     public static string toutf8string(string s) {
      stringbuffer sb = new stringbuffer();
      for (int i=0;i<s.length();i++) {
       char c = s.charat(i);
       if (c >= 0 && c <= 255) {
        sb.append(c);
       } else {
        byte[] b;
        try {
         b = character.tostring(c).getbytes("utf-8");
        } catch (exception ex) {
         system.out.println(ex);
         b = new byte[0];
        }
        for (int j = 0; j < b.length; j++) {
         int k = b[j];
         if (k < 0) k += 256;
         sb.append("%" + integer.tohexstring(k).touppercase());
        }
       }
      }
      string s_utf8 = sb.tostring();
      sb.delete(0,sb.length());
      sb.setlength(0);
      sb = null;
      return s_utf8;
     }
     file://取得下载文件的真实全路径名称
     private string getrealname(httpservletrequest request,string file_name) {
      if(request==null || file_name==null) return null;
      file_name = file_name.trim();
      if(file_name.equals("")) return null;
     
      string file_path = request.getrealpath(file_name);
      if ( file_path== null) return null;
      file file = new file(file_path);
      if (!file.exists()) return null;
      return file_path;
     }
     file://实现下载
     public void doget(httpservletrequest request,httpservletresponse response) throws servletexception {
      string file_name = request.getparameter("file_name");
      if(file_name==null) file_name = "";
      file_name = file_name.trim();
     
      inputstream instream= null;
      string attch_name = "";
     
      byte[] b  = new byte[100];
      int    len= 0;
      try {
       file://取得附件的名称
       attch_name = getattachname(file_name);
      
       file_name  = getrealname(request,file_name);
       if(file_name==null) {
        system.out.println("文件不存在,或者禁止下载");
        return ;
       }
       attch_name = toutf8string(attch_name);
       file://读到流中
       instream=new fileinputstream(file_name);
       file://设置输出的格式
       response.reset();
       response.setcontenttype("application/x-msdownload");
      
      
       response.addheader("content-disposition","attachment; filename=/"" + attch_name + "/"");
       file://循环取出流中的数据
       while((len=instream.read(b)) >0) {
        response.getoutputstream().write(b,0,len);
       }
       instream.close();
      }catch ( exception e ){
       if ( e instanceof java.io.filenotfoundexception ) {
        try {
         response.sendredirect("/tip/file_not_found.html");
        }
        catch ( ioexception ex ) {
         ex.printstacktrace(system.err);
        }
       }
       else {
        e.printstacktrace(system.err);
       }
      }
     }
    }

    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表