jsp文件下载功能实现代码

本文实例为大家分享了jsp实现文件下载功能的3种方法,供大家参考,具体内容如下

第一种、采用转发的方式:

package cn.jbit.download.servlet; 
 
import java.io.ioexception; 
 
import javax.servlet.requestdispatcher; 
import javax.servlet.servletexception; 
import javax.servlet.http.httpservlet; 
import javax.servlet.http.httpservletrequest; 
import javax.servlet.http.httpservletresponse; 
 
public class downloadservlet extends httpservlet { 
 
  private static final long serialversionuid = 6765085208899952414l; 
 
  public void doget(httpservletrequest request, httpservletresponse response) 
      throws servletexception, ioexception { 
    dopost(request, response); 
  } 
 
  public void dopost(httpservletrequest request, httpservletresponse response) 
      throws servletexception, ioexception { 
    string filedownload = "/upload/1/10213.jpg";//即将下载的文件的相对路径 
    string filedisplay = "10213.jpg";//下载文件时显示的文件保存名称 
    response.setcontenttype("application/x-download");//设置为下载application/x-download 
    //response.setcontenttype("application/x-msdownload");//设置为下载application/x-msdownload 
    //response.setcontenttype("application/octet-stream");//设置为下载application/octet-stream 
    response.addheader("content-disposition", "attachment;filename=" 
        + filedisplay); 
     
    try { 
      requestdispatcher rd = request.getrequestdispatcher(filedownload); 
      if(rd != null) 
      { 
        rd.forward(request,response); 
      } 
      response.flushbuffer(); 
    } catch (exception e) { 
      e.printstacktrace(); 
    } 
  } 
} 

二、通过输出流的方式:

package cn.jbit.download.servlet; 
 
import java.io.bufferedinputstream; 
import java.io.bufferedoutputstream; 
import java.io.file; 
import java.io.fileinputstream; 
import java.io.ioexception; 
import java.io.inputstream; 
import java.io.outputstream; 
 
import javax.servlet.servletexception; 
import javax.servlet.http.httpservlet; 
import javax.servlet.http.httpservletrequest; 
import javax.servlet.http.httpservletresponse; 
 
public class downloadofioservlet extends httpservlet { 
 public void doget(httpservletrequest request, httpservletresponse response) 
      throws servletexception, ioexception { 
    dopost(request, response); 
  } 
   
  public void dopost(httpservletrequest request, httpservletresponse response) 
      throws servletexception, ioexception { 
    string basepath = request.getsession().getservletcontext().getrealpath("/upload"); 
 
    string filedisplay = "helloworld.jpg"; 
    string filedownload = basepath + file.separator + "helloworld.jpg"; 
    response.setcontenttype("applicaiton/x-download"); 
    response.addheader("content-disposition", "attachment;filename="+filedisplay); 
     
    inputstream is = null; 
    outputstream os = null; 
    bufferedinputstream bis = null; 
    bufferedoutputstream bos = null; 
     
    is = new fileinputstream(new file(filedownload)); 
    bis = new bufferedinputstream(is); 
    os = response.getoutputstream(); 
    bos = new bufferedoutputstream(os); 
     
    byte[] b = new byte[1024]; 
    int len = 0; 
    while((len = bis.read(b)) != -1){ 
      bos.write(b,0,len); 
    } 
     
    bis.close(); 
    is.close(); 
    bos.close(); 
    os.close(); 
  } 
} 


第三种、通过超链接的方式(注意不推荐,因为会暴露下载文件的位置)

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

相关文章