Javaweb编程之Response下载文件
#日常分享所得,欢迎关注学习计算机、热爱计算机的我!后续更精彩#
关于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) throws IOException; 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); } }