Web文件下載有兩種,一種是文件在網站目錄下,在瀏覽器中直接輸入文件路徑即可下載,如http://www.xxx.com/file.zip。另外一種是文件不在網站目錄下或者文件是動態生成的(導出報表或者導出excel等),這種情況需要通過response的OutputStream實現文件的下載。DownloadUtils是一個Java Web文件下載工具類,提供多種靜態方法實現文件下載。
package com.rhui.util; 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 java.net.URLEncoder; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; /** * 文件下載類 */ public class DownloadUtils { /** * 文件下載編碼 * 該編碼告訴瀏覽器文件名的編碼方式,以防下載中文文件名時有亂碼 */ private static String encoding = "utf-8"; /** * 文件下載 * @param response * @param filePath 文件在服務器上的路徑,包含文件名 */ public static void download(HttpServletResponse response, String filePath){ File file = new File(filePath.toString()); download(response, file, null, encoding); } /** * 文件下載 * @param response * @param filePath 文件在服務器上的路徑,包括文件名稱 * @param fileName 文件下載到瀏覽器的名稱,如果不想讓瀏覽器下載的文件名稱和服務器上的文件名稱一樣,請設置該參數 */ public static void download(HttpServletResponse response, String filePath, String fileName){ File file = new File(filePath.toString()); download(response, file, fileName, encoding); } /** * 文件下載 * @param response * @param filePath 文件在服務器上的路徑,包括文件名稱 * @param fileName 文件下載到瀏覽器的名稱,如果不想讓瀏覽器下載的文件名稱和服務器上的文件名稱一樣,請設置該參數 * @param encoding 文件名稱編碼 */ public static void download(HttpServletResponse response, String filePath, String fileName, String encoding){ File file = new File(filePath.toString()); download(response, file, fileName, encoding); } /** * 文件下載 * @param response * @param file 文件 * @param fileName 文件下載到瀏覽器的名稱,如果不想讓瀏覽器下載的文件名稱和服務器上的文件名稱一樣,請設置該參數 */ public static void download(HttpServletResponse response, File file) { download(response, file, null, encoding); } /** * 文件下載 * @param response * @param file 文件 * @param fileName 文件下載到瀏覽器的名稱,如果不想讓瀏覽器下載的文件名稱和服務器上的文件名稱一樣,請設置該參數 */ public static void download(HttpServletResponse response, File file, String fileName) { download(response, file, fileName, encoding); } /** * 文件下載 * @param response * @param file 文件 * @param fileName 文件下載到瀏覽器的名稱,如果不想讓瀏覽器下載的文件名稱和服務器上的文件名稱一樣,請設置該參數 * @param encoding 文件名稱編碼 */ public static void download(HttpServletResponse response, File file, String fileName, String encoding) { if(file == null || !file.exists() || file.isDirectory()){ return; } // 如果不指定文件下載到瀏覽器的名稱,則使用文件的默認名稱 if (StringUtils.isBlank(fileName)) { fileName = file.getName(); } try { InputStream is = new FileInputStream(file); download(response, is, fileName, encoding); } catch (IOException e) { e.printStackTrace(); } } /** * 文件下載 * @param response * @param is 文件輸入流 * @param fileName 下載的文件名稱 * @throws IOException */ public static void download(HttpServletResponse response, InputStream is, String fileName){ download(response, is, fileName, encoding); } /** * 文件下載 * @param response * @param is 文件輸入流 * @param fileName 下載的文件名稱 * @param encoding 編碼格式 */ public static void download(HttpServletResponse response, InputStream is, String fileName, String encoding){ if(is == null || StringUtils.isBlank(fileName)){ return; } BufferedInputStream bis = null; OutputStream os = null; BufferedOutputStream bos = null; try{ bis = new BufferedInputStream(is); os = response.getOutputStream(); bos = new BufferedOutputStream(os); response.setContentType("application/octet-stream;charset=" + encoding); response.setCharacterEncoding(encoding); response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(fileName, encoding)); byte[] buffer = new byte[1024]; int len = bis.read(buffer); while(len != -1){ bos.write(buffer, 0, len); len = bis.read(buffer); } bos.flush(); }catch(IOException e){ e.printStackTrace(); }finally{ if(bis != null){ try{ bis.close(); }catch(IOException e){} } if(is != null){ try{ is.close(); }catch(IOException e){} } } } public static String getEncoding() { return encoding; } public static void setEncoding(String encoding) { DownloadUtils.encoding = encoding; } } 如果文件保存在服務器的非網站目錄下
String filePath = "c://file.zip"; DownloadUtils.download(response, filePath);
如果文件是輸入流
// is為文件輸入流 // fileName為瀏覽器下載的文件名稱 // encoding為文件名稱編碼,預防文件中有中文的時候產生亂碼 String fileName = "file.zip"; String encoding = "utf-8"; DownloadUtils.download(response, is, fileName, encoding);
Servlet中文件下載
package com.rhui.web.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.rhui.util.DownloadUtils; @WebServlet("/download/servlet") public class DownloadServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filePath = "c://file.zip"; DownloadUtils.download(response, filePath); } } PS:圖片下載(含防盜鏈功能)
package cn.itcast.day06.web.servlet; import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URLEncoder; import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; public class DownloadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 實現防盜鏈功能 // 獲得 referer 頭 用于說明來訪者來自哪里 String referer = request.getHeader("referer"); if(referer==null || !referer.startsWith("http://localhost")) { // 是盜鏈者 response.sendRedirect("/day06/index.jsp"); return ; } // 解決response中文亂碼問題 response.setContentType("text/html;charset=utf-8"); // 設置消息體的編碼 // 通過 http 協議 發送的http響應消息頭 不能出現中文 中文必須要經過url編碼 String filename = URLEncoder.encode("美女.jpg", "utf-8"); // 通知瀏覽器以下載的方式讀取資源 response.setHeader("content-disposition", "attachment;filename="+filename); // 讀取圖片數據 發給ie瀏覽器 String webPath = "/download/美女.jpg"; // 相當于當前web應用的path ServletContext servletContext = super.getServletContext(); InputStream in = servletContext.getResourceAsStream(webPath); OutputStream out = response.getOutputStream(); int len; byte[] buffer = new byte[1024]; while((len=in.read(buffer))!=-1) out.write(buffer, 0, len); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }新聞熱點
疑難解答