"JSP" writes Java to the respond output stream for users to download

Source: Internet
Author: User

Output the contents of Java to a file everyone will, the input and output stream is the necessary lesson for every Java program ape. In the "Java" Print stream and buffer reader to complete the input and output to file operations (click the Open link) and "Java" input and output and JDK1.5 after the new string StringBuilder "(click Open link), I have said this question. But on JSP, how to write the contents of Java directly to the Web page for users to download it? Generally some of the export function implementation, it is necessary to do so.

Specific example:


The user accesses download this servlet, directly provides a "Chinese is no problem" txt "file to the user to download. The content is written directly to the respond output stream in Java, and there is no such file on the server at all. Chinese is no problem in the "hehe" in the user access download this servlet is constructed.

You can write the following servlet without any plug-ins, and construct a respond output stream to print the content printwriter the print stream.

The rest of what plug-in output to the file similarly, you can change its write method is OK.

The following servlet uses the Servlet3.0 notation, indicating that this servlet's access address is/download, the test is not written localhost:8080/project name/download/, this is not possible, there is no trailing slash, The service method is a collection of the original Dopost and Doget methods, which is generally not written like this:

Import Java.io.*;import java.net.*;import Javax.servlet.*;import Javax.servlet.http.*;import javax.servlet.annotation.*; @SuppressWarnings ("Serial") @WebServlet (name = "Download", Urlpatterns = {"/download"}) public class Download extends HttpServlet {protected void service (HttpServletRequest request,httpservletresponse Response) throws Servletexception, IOException {//In the SSH framework, can be HttpServletResponse response= Servletactioncontext.getresponse (); Remove the Respond object//Clear the Response object, otherwise the Response.reset () of what is cached;// Indicate that this is a downloaded Respondresponse.setcontenttype ("application/x-download");//here is the default filename provided to the user string filename = "Chinese is no problem for. txt" ";//double decode, prevent garbled filename = urlencoder.encode (filename," UTF-8 "); Response.setcharacterencoding (" UTF-8 "); Response.AddHeader ("Content-disposition", "attachment;filename=" + filename);/* * This is the most critical step. * Write this thing directly into the response output stream and download it to the user. * If output to C:\b.txt is: * printwriter printwriter = new PrintWriter (New FileWriter ("C:\\b.txt", true)); * Now write a good respond head, wrote: * printwriter printwriter = new PrintWriter(Response.getoutputstream ()); * Change the output point of the printwriter to */printwriter printwriter = new PrintWriter (Response.getoutputstream ());p rintwriter.println (" Oh! ")///print stream all output content, must close this print stream to be valid Printwriter.close ();}}


"JSP" writes Java to the respond output stream for users to download

Related Article

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.