Javaweb程式設計之Response下載檔案
阿新 • • 發佈:2020-07-06
#日常分享所得,歡迎關注學習計算機、熱愛計算機的我!後續更精彩#
關於HttpServletResponse
當web伺服器接收到客戶端的http請求時,伺服器針對這個請求,會分別建立一個代表請求的物件,叫做HttpServletRequest;一個代表響應的物件,叫做HttpServletResponse;
簡單理解就是:
-
如果需要獲取客戶端請求過來的引數,使用HttpServletRequest;
-
如果要給提供給客戶端一些響應資訊,則使用HttpServletResponse。
關於HttpServletResponse的簡單分類:
-
負責向客戶端瀏覽器傳送一些資料的方法:
ServletOutputStream getOutputStream() throws IOException; PrintWriter getWriter() throws IOException;
-
負責向瀏覽器傳送一些響應頭的方法:
void sendError(int var1, String var2) throws IOException; void sendError(int var1) throws IOException; void sendRedirect(String var1) throwsIOException; void setDateHeader(String var1, long var2); void addDateHeader(String var1, long var2); void setHeader(String var1, String var2); void addHeader(String var1, String var2); void setIntHeader(String var1, int var2); void addIntHeader(String var1, int var2); void setStatus(int var1);
-
負責響應的狀態碼
int SC_CONTINUE = 100; int SC_SWITCHING_PROTOCOLS = 101; int SC_OK = 200; //常見 int SC_CREATED = 201; int SC_ACCEPTED = 202; int SC_NON_AUTHORITATIVE_INFORMATION = 203; int SC_NO_CONTENT = 204; int SC_RESET_CONTENT = 205; int SC_PARTIAL_CONTENT = 206; int SC_MULTIPLE_CHOICES = 300; //常見 int SC_MOVED_PERMANENTLY = 301; int SC_MOVED_TEMPORARILY = 302; int SC_FOUND = 302; int SC_SEE_OTHER = 303; int SC_NOT_MODIFIED = 304; int SC_USE_PROXY = 305; int SC_TEMPORARY_REDIRECT = 307; int SC_BAD_REQUEST = 400; int SC_UNAUTHORIZED = 401; int SC_PAYMENT_REQUIRED = 402; int SC_FORBIDDEN = 403; int SC_NOT_FOUND = 404; //常見 int SC_METHOD_NOT_ALLOWED = 405; int SC_NOT_ACCEPTABLE = 406; int SC_PROXY_AUTHENTICATION_REQUIRED = 407; int SC_REQUEST_TIMEOUT = 408; int SC_CONFLICT = 409; int SC_GONE = 410; int SC_LENGTH_REQUIRED = 411; int SC_PRECONDITION_FAILED = 412; int SC_REQUEST_ENTITY_TOO_LARGE = 413; int SC_REQUEST_URI_TOO_LONG = 414; int SC_UNSUPPORTED_MEDIA_TYPE = 415; int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416; int SC_EXPECTATION_FAILED = 417; int SC_INTERNAL_SERVER_ERROR = 500; //常見 int SC_NOT_IMPLEMENTED = 501; int SC_BAD_GATEWAY = 502; int SC_SERVICE_UNAVAILABLE = 503; int SC_GATEWAY_TIMEOUT = 504; int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
關於響應狀態碼:平時在網頁瀏覽是經常會遇到404錯誤,500錯誤等等,這些就稱作響應狀態碼。
HttpServletResponse的常見應用
-
向瀏覽器輸入一些資料;
-
下載檔案
-
準備需要下載檔案的路徑
-
準備需要下載的檔案的檔名
-
設定瀏覽器的支援下載
-
獲取到下載到的檔案的輸入流
-
建立一個緩衝區用來寫入流
-
獲取OutputStream物件
-
將FileOutputStream物件的流寫入到先前建立的buffer緩衝區中
-
使用OutputStream物件將buffer緩衝區中的資料輸出到自己的客戶端中
-
示例:
配置xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0" metadata-complete="true"> <servlet> <servlet-name>fileDownload</servlet-name> <servlet-class>psl.wong.servlet.FileServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>fileDownload</servlet-name> <url-pattern>/down</url-pattern> </servlet-mapping> </web-app>
主要程式碼:
package psl.wong.servlet; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.FileInputStream; import java.io.IOException; import java.net.URLEncoder; public class FileServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //下載檔案 //- 準備需要下載檔案的路徑 String realPath = "D:\\Program Files\\ideaIU\\IdeaWorkspace\\Javaweb_02_Servlet\\Response\\src\\main\\resources\\騰訊.PNG"; //原始檔URL System.out.println("下載檔案的路徑:"+realPath); //- 準備需要下載的檔案的檔名 String fileName = realPath.substring(realPath.lastIndexOf("\\")+1); //獲取到最後一個”/“之後的資料,即為檔名。 //- 設定瀏覽器的支援下載 resp.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName,"UTF-8")); //- 獲取到下載到的檔案的輸入流 FileInputStream in = new FileInputStream(realPath); //- 建立一個緩衝區用來寫入流 int len=0; byte[] bytes = new byte[1024]; //- 獲取OutputStream物件 ServletOutputStream outputStream = resp.getOutputStream(); //- 將FileOutputStream物件的流寫入到先前建立的buffer緩衝區中,使用OutputStream物件將buffer緩衝區中的資料輸出到自己的客戶端中 while ((len=in.read(bytes))>0){ outputStream.write(bytes,0,len); } outputStream.close(); in.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }