File Download-getrequestdispatcher and file stream output method

Source: Internet
Author: User

From: http://blog.csdn.net/xiangxiaofeng12/article/details/5564756

String Path = "/index. jsp"; // This is the URL of an absolute path in the current application.
Servlet:
Requestdispatcher dispatcher = getservletcontext (). getrequestdispatcher (PATH );
JSP:
Requestdispatcher dispatcher = application. getrequestdispatcher (PATH );

ALL:
Dispatcher. Forward (); // go to the path page (no other output can be provided before or after this page)
Dispatcher. Include (); // output the execution result of the page path to the browser (other outputs can be made before or after this)

1. servletrequest. getrequestdispatcher (string path)
Path can be an absolute or relative path.

2. servletcontext. getrequestdispatcher (string path)
The path must start with "/", indicating the context Root

3. Another method is servletcontext. getnamedispatcher (string name)
The parameter is not a path, but its name. If multiple servlets have the same name, the configuration is different in Web. xml.

4. The above method returns a requestdispatcher object, and then forward () or include ()

5. The difference between forward () and include () is that after the include () method forwards the HTTP request to another servlet or JSP, the servlet or JSP can process the request, however, the original servlet or JSP is dominant, that is, the called servlet or JSP will be incorporated into the original httpresponse object if any HTTP response is generated.

 

<% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java" Import = "Java. SQL. * "errorpage =" "%> <% @ page import =" java.net. urlencoder "%> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

 

 

void downLoaded(String fileName, HttpServletRequest request,            HttpServletResponse response) throws IOException {        response.setHeader("Content-Disposition", "attachment;filename="                + URLEncoder.encode(fileName, "UTF-8"));        response.setContentType("application/x-download;charset=UTF-8");        // response.setContentType("application/octet-stream;charset=UTF-8");        ServletOutputStream outputStream = response.getOutputStream();        BufferedInputStream inputStream = new BufferedInputStream(                new FileInputStream(fileName));        int size = 2048;        byte[] data = new byte[size];        while ((size = inputStream.read(data, 0, data.length)) != 0) {            outputStream.write(data, 0, size);            outputStream.flush();        }    }

 

Supplement:

Problem getoutputstream () has already been called for this response, see:

Http://qify.iteye.com/blog/747842

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.