jsp文件下載功能實(shí)現(xiàn)代碼

本文實(shí)例為大家分享了jsp實(shí)現(xiàn)文件下載功能的3種方法,供大家參考,具體內(nèi)容如下

第一種、采用轉(zhuǎn)發(fā)的方式:

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";//即將下載的文件的相對(duì)路徑 
    string filedisplay = "10213.jpg";//下載文件時(shí)顯示的文件保存名稱 
    response.setcontenttype("application/x-download");//設(shè)置為下載application/x-download 
    //response.setcontenttype("application/x-msdownload");//設(shè)置為下載application/x-msdownload 
    //response.setcontenttype("application/octet-stream");//設(shè)置為下載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(); 
  } 
} 


第三種、通過超鏈接的方式(注意不推薦,因?yàn)闀?huì)暴露下載文件的位置)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持碩編程。

相關(guān)文章
亚洲国产精品第一区二区,久久免费视频77,99V久久综合狠狠综合久久,国产免费久久九九免费视频