Servlet實(shí)現(xiàn)文件下載功能
本文實(shí)例為大家分享了Servlet實(shí)現(xiàn)文件下載的具體代碼,供大家參考,具體內(nèi)容如下
把文件目錄直接暴露給用戶是很不安全的。所以要用Servlet來做,而且這樣做,文件的存儲方式就更豐富了,可以是從文件系統(tǒng)上取來的,也可以是數(shù)據(jù)庫中經(jīng)過計(jì)算生成的,或者從其它什么稀奇古怪的地方取來的。
public class DownloadServlet extends HttpServlet { private String contentType = "application/x-msdownload"; private String enc = "utf-8"; private String fileRoot = ""; /** * 初始化contentType,enc,fileRoot */ public void init(ServletConfig config) throws ServletException { String tempStr = config.getInitParameter("contentType"); if (tempStr != null && !tempStr.equals("")) { contentType = tempStr; } tempStr = config.getInitParameter("enc"); if (tempStr != null && !tempStr.equals("")) { enc = tempStr; } tempStr = config.getInitParameter("fileRoot"); if (tempStr != null && !tempStr.equals("")) { fileRoot = tempStr; } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filepath = request.getParameter("filepath"); String fullFilePath = fileRoot + filepath; /*讀取文件*/ File file = new File(fullFilePath); /*如果文件存在*/ if (file.exists()) { String filename = URLEncoder.encode(file.getName(), enc); response.reset(); response.setContentType(contentType); response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); int fileLength = (int) file.length(); response.setContentLength(fileLength); /*如果文件長度大于0*/ if (fileLength != 0) { /*創(chuàng)建輸入流*/ InputStream inStream = new FileInputStream(file); byte[] buf = new byte[4096]; /*創(chuàng)建輸出流*/ ServletOutputStream servletOS = response.getOutputStream(); int readLength; while (((readLength = inStream.read(buf)) != -1)) { servletOS.write(buf, 0, readLength); } inStream.close(); servletOS.flush(); servletOS.close(); } } }
web.xml
<servlet> <servlet-name>downloadservlet-name> <servlet-class>org.mstar.servlet.DownloadServletservlet-class> <init-param> <param-name>fileRootparam-name> <param-value>d:/tempparam-value> init-param> <init-param> <param-name>contentTypeparam-name> <param-value>application/x-msdownloadparam-value> init-param> <init-param> <param-name>encparam-name> <param-value>utf-8param-value> init-param> servlet> <servlet-mapping> <servlet-name>downloadservlet-name> <url-pattern>/downurl-pattern> servlet-mapping>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring-webflux訪問關(guān)系型數(shù)據(jù)庫實(shí)戰(zhàn)
這篇文章主要為大家介紹了Spring-webflux訪問關(guān)系型數(shù)據(jù)庫實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07xxl-job 帶參數(shù)執(zhí)行和高可用部署方法
這篇文章主要介紹了xxl-job 帶參數(shù)執(zhí)行和高可用部署,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04springboot 按月分表的實(shí)現(xiàn)方式
本文主要介紹了springboot 按月分表的實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Java編程用棧來求解漢諾塔問題的代碼實(shí)例(非遞歸)
這篇文章主要介紹了Java編程用棧來求解漢諾塔問題的代碼實(shí)例(非遞歸),具有一定參考價值,這里給大家分享下,供朋友們參考。2017-10-10Java中對象?和?json?互轉(zhuǎn)四種方式?json-lib、Gson、FastJson、Jackson
這篇文章主要介紹了Java中對象?和?json?互轉(zhuǎn)?四種方式?json-lib、Gson、FastJson、Jackson,需要的朋友可以參考下2023-11-11springboot用thymeleaf模板的paginate分頁完整代碼
本文根據(jù)一個簡單的user表為例,展示 springboot集成mybatis,再到前端分頁完整代碼,需要的朋友可以參考下2017-07-07java設(shè)計(jì)模式之工廠模式實(shí)例詳解
這篇文章主要介紹了java設(shè)計(jì)模式之工廠模式,結(jié)合具有實(shí)例形式分析了java工廠模式的概念、原理、實(shí)現(xiàn)與使用方法,需要的朋友可以參考下2017-09-09