Servlet | 访问不同格式文件(PDF、doc)

 

 核心代码

//设置响应内容类型为PDF类型
response.setContentType("application/pdf");                    
request.getRequestDispatcher("/WEB-INF/搜索框架.pdf").forward(request,response);

 

示例:

 1 import javax.servlet.ServletException;
 2 import javax.servlet.ServletOutputStream;
 3 import javax.servlet.annotation.WebServlet;
 4 import javax.servlet.http.HttpServlet;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 
 8 @WebServlet(value="/getFile")
 9 public class FilePage extends HttpServlet{
10 
11     @Override
12     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
13         processRequest(req, resp);
14     }
15     
16     
17     @Override
18     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
19         processRequest(req, resp);
20     }
21     
22     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
23             throws ServletException, IOException{
24         
25         //设置响应内容类型为PDF类型
26         response.setContentType("application/pdf");                    
27         request.getRequestDispatcher("/WEB-INF/搜索框架.pdf").forward(request,response);
28         
29         
30     }
31 
32 
33 }

 

 

 

 

1. 如何用 servlet 打开非 HTML 格式的文档(转)

  http://study.qqcf.com/web/598/149782.htm

 2. 访问WEB-INF目录中的JSP文件

  https://blog.csdn.net/zht666/article/details/8690669

posted @ 2018-10-18 18:57  听说这是最长的名字了  阅读(512)  评论(0编辑  收藏  举报