一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

servlet實現文件下載的實用類分享

復制代碼 代碼如下:
package com;

import Java.io.IOException;
import Java.io.PrintWriter;
import Java.NET.URLEncoder;
import Java.util.Date;

import Javax.servlet.ServletException;
import Javax.servlet.http.HttpServlet;
import Javax.servlet.http.HttpServletRequest;
import Javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sun.xml.internal.messaging.saaj.packaging.mime.interNET.MimeUtility;


/**
 * 文件下載類。為了防止客戶端瀏覽器直接打開目標文件(例如在裝了MS Office套件的Windows中的IE瀏覽器可能就會直接在IE瀏覽器中打開你想下載的doc或者xls文件),在響應頭里加入強制下載的MIME類型。
 */
public class DownloadFile extends HttpServlet {
 private static final Log log = LogFactory.getLog(DownloadFile.class);

 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
  long timeStart = 0;
  if(log.isDebugEnabled()){
   timeStart=System.currentTimeMillis();
  }
  response.setContentType("application/x-download charset=UTF-8");
  Java.io.FileInputStream fis = null;
  String filepath = request.getRealPath("");
  Javax.servlet.ServletOutputStream sos = null;
  // System.out.println("DownloadFile filename:" + filename);
  try {
   if(request.getParameter("filename")==null
     ||request.getParameter("showName")==null){
    return;
   }
   String filename = request.getParameter("filename");
   String showName = request.getParameter("showName");
   request.setCharacterEncoding("UTF-8");
   response.setCharacterEncoding("UTF-8");
   Java.io.File file = new Java.io.File(filepath + filename);
   if (!file.exists()) {
    log.error(file.getAbsolutePath() + " 文件不存在!");
    return;
   }
   // 讀取文件流
   fis = new Java.io.FileInputStream(file);
   // 設置下載保存的文件名
   sos = response.getOutputStream();
   showName += filename.substring(filename.lastIndexOf("."));
   String contentDisposition = "", browser = getBrowser(request);
   if ("IE".equals(browser)) {
    contentDisposition = "attachment; filename=" + URLEncoder.encode(showName, "UTF-8").replace("+", "%20");
   } else if ("CH".equals(browser)) {
    contentDisposition = "attachment; filename=" + MimeUtility.encodeText(showName, "UTF8", "B");
   } else if ("SF".equals(browser)) {
    contentDisposition = "attachment; filename=" + new String(showName.getBytes("UTF-8"), "ISO8859-1");
   } else {
    contentDisposition = "attachment; filename*=UTF-8''" + URLEncoder.encode(showName, "UTF-8").replace("+", "%20");
   }
   response.setHeader("Content-Disposition", contentDisposition);
   int byteCount = 0;
   if (fis != null) {
    byte[] buff = new byte[1024];
    int bytesRead;
    while(-1 != (bytesRead = fis.read(buff, 0, buff.length))) {
     sos.write(buff, 0, bytesRead);
     sos.flush();
     byteCount += bytesRead;
    }
   }
   sos.flush();
   if(log.isDebugEnabled()){
    log.debug("文件下載完成,文件大?。?+ byteCount +",總共用時:"+ (new Date().getTime() - timeStart) +"毫秒。");
   }
  } catch(IOException ioe) {
   ioe.printStackTrace();
  } finally {
   try {
    if(fis!=null){
     fis.close();
    }
   } catch (IOException e) {
   } finally {
    try {
     if(sos!=null){
      sos.close();
     }
    } catch (IOException e) {
    }
   }
  }
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("<!DOCTYPE html PUBLIC /"-//W3C//DTD XHTML 1.0 Transitional//EN/" /"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd/">");
  out.println("<html>");
  out.println(" <head>");
  out.println("   <title>文件下載</title>");
  out.println("   <meta http-equiv=/"Content-Type/" content=/"text/html; charset=UTF-8/" />");
  out.println(" </head>");
  out.println(" <body>");
  out.print(" This is ");
  out.print(this.getClass().getName());
  out.println(", using the POST method");
  out.println(" </body>");
  out.println("</html>");
  out.flush();
  out.close();
 }

 private String getBrowser(HttpServletRequest request) {
  String userAgent = request.getHeader("USER-AGENT").toLowerCase();
  if (userAgent != null) {
   if (userAgent.indexOf("msie") >= 0) {
    return "IE";
   } else if (userAgent.indexOf("mozilla") >= 0) {
    return "FF";
   } else if (userAgent.indexOf("applewebkit") >= 0) {
    return "CH";
   } else if (userAgent.indexOf("safari") >= 0) {
    return "SF";
   } else if (userAgent.indexOf("opera") >= 0) {
    return "OP";
   }
  }
  return null;
 }
}

jsp技術servlet實現文件下載的實用類分享,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产精品特级毛片一区二区三区 | 色黄啪啪网18勿进 | 久久国产精品99久久久久久牛牛 | 一区二区三区午夜 | 国产精品免费一区二区三区四区 | 91aaa免费观看在线观看资源 | 美女色影院 | 五月天亚洲综合 | 色婷婷在线视频观看 | 四虎国产精品一区二区 | 小视频免费在线观看 | 人人爱天天做夜夜爽2020麻豆 | 亚洲福利视频网站 | 免费看91视频 | 亚洲黄色小视频 | 亚洲激情在线观看 | 最新精品视频在线观看 | 国产欧美久久久另类精品 | 国产激情在线 | 好吊妞在线播放 | 免费污视频在线观看 | 亚洲天堂一区二区在线观看 | 国产精品一区二区久久精品涩爱 | 成人a视频在线观看 | 国产中日韩一区二区三区 | 91色在线观看国产 | 2021中国大陆精品视频xxxx | 久久久噜噜噜久久久午夜 | 狠狠色丁香婷婷久久综合不卡 | 黄色大片网站在线观看 | 亚洲国产精品婷婷久久 | 最新精品亚洲成a人在线观看 | 五月婷婷激情综合 | 国产精品大白天新婚身材 | 免费人成在线蜜桃视频 | 婷婷久草 | 一本久道热中字伊人 | 国产视频福利 | 国产精品好好热在线观看 | 四虎国产精品永久一区 | 亚洲一区在线视频观看 |