欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Java文件下载的几种实现方式

程序员文章站 2024-03-09 08:23:59
java文件下载的几种方式,具体如下: public httpservletresponse download(string path, httpservle...

java文件下载的几种方式,具体如下:

public httpservletresponse download(string path, httpservletresponse response) {
    try {
      // path是指欲下载的文件的路径。
      file file = new file(path);
      // 取得文件名。
      string filename = file.getname();
      // 取得文件的后缀名。
      string ext = filename.substring(filename.lastindexof(".") + 1).touppercase();

      // 以流的形式下载文件。
      inputstream fis = new bufferedinputstream(new fileinputstream(path));
      byte[] buffer = new byte[fis.available()];
      fis.read(buffer);
      fis.close();
      // 清空response
      response.reset();
      // 设置response的header
      response.addheader("content-disposition", "attachment;filename=" + new string(filename.getbytes()));
      response.addheader("content-length", "" + file.length());
      outputstream toclient = new bufferedoutputstream(response.getoutputstream());
      response.setcontenttype("application/octet-stream");
      toclient.write(buffer);
      toclient.flush();
      toclient.close();
    } catch (ioexception ex) {
      ex.printstacktrace();
    }
    return response;
  }

  public void downloadlocal(httpservletresponse response) throws filenotfoundexception {
    // 下载本地文件
    string filename = "operator.doc".tostring(); // 文件的默认保存名
    // 读到流中
    inputstream instream = new fileinputstream("c:/operator.doc");// 文件的存放路径
    // 设置输出的格式
    response.reset();
    response.setcontenttype("bin");
    response.addheader("content-disposition", "attachment; filename=\"" + filename + "\"");
    // 循环取出流中的数据
    byte[] b = new byte[100];
    int len;
    try {
      while ((len = instream.read(b)) > 0)
        response.getoutputstream().write(b, 0, len);
      instream.close();
    } catch (ioexception e) {
      e.printstacktrace();
    }
  }

  public void downloadnet(httpservletresponse response) throws malformedurlexception {
    // 下载网络文件
    int bytesum = 0;
    int byteread = 0;

    url url = new url("windine.blogdriver.com/logo.gif");

    try {
      urlconnection conn = url.openconnection();
      inputstream instream = conn.getinputstream();
      fileoutputstream fs = new fileoutputstream("c:/abc.gif");

      byte[] buffer = new byte[1204];
      int length;
      while ((byteread = instream.read(buffer)) != -1) {
        bytesum += byteread;
        system.out.println(bytesum);
        fs.write(buffer, 0, byteread);
      }
    } catch (filenotfoundexception e) {
      e.printstacktrace();
    } catch (ioexception e) {
      e.printstacktrace();
    }
  }

 //支持在线打开文件的一种方式

public void download(string filepath, httpservletresponse response, boolean isonline) throws exception {
    file f = new file(filepath);
    if (!f.exists()) {
      response.senderror(404, "file not found!");
      return;
    }
    bufferedinputstream br = new bufferedinputstream(new fileinputstream(f));
    byte[] buf = new byte[1024];
    int len = 0;

    response.reset(); // 非常重要
    if (isonline) { // 在线打开方式
      url u = new url("file:///" + filepath);
      response.setcontenttype(u.openconnection().getcontenttype());
      response.setheader("content-disposition", "inline; filename=" + f.getname());
      // 文件名应该编码成utf-8
    } else { // 纯下载方式
      response.setcontenttype("application/x-msdownload");
      response.setheader("content-disposition", "attachment; filename=" + f.getname());
    }
    outputstream out = response.getoutputstream();
    while ((len = br.read(buf)) > 0)
      out.write(buf, 0, len);
    br.close();
    out.close();
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。