[Jsp] Write Java into the Respond output stream for users to download
You can output the content in Java to a file. The input/output stream is a required course for every Java program. Complete input and output to file operations in the [Java] print stream and buffer reader (click to open the link) I have mentioned this problem in [Java] input and output and the new string StringBuilder after JDK1.5 (click to open the link. But on Jsp, how does one directly write the Java content to the webpage for users to download? This is generally required for some export functions.
For example:
When the user downloads downloadthis servletv, the user directly downloads a file with no question .txt in Chinese. The content is directly written to the Respond output stream in Java, and this file is not available on the server. There is no question in Chinese. "Haha" is constructed only when the user accesses the download Servlet.
You can write the following Servlet to construct a Respond output stream to print the PrintWriter content without any plug-ins.
For other plug-ins that output data to files, you can change the write method to OK.
The following Servlet uses Servlet3.0 to indicate that the access address of this Servlet is/download. Do not write the test as localhost: 8080/project name/download/. This is not acceptable, there is no slash behind it. 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, you can use HttpServletRespons E response = ServletActionContext. getResponse (); get the Respond object // clear the response object; otherwise, the cached response is displayed. reset (); // indicates that this is a downloaded respondresponse. setContentType ("application/x-download"); // The default file name String filename = "TXT with no question in Chinese" is provided here; // Double decoding prevents garbled filename = URLEncoder. encode (filename, "UTF-8"); response. setCharacterEncoding ("UTF-8"); response. addHeader ("Content-Disposition", "attachment; filename =" + filename);/** this is the most important Key Step. * Write this item directly to the response output stream and download it for the user. * If the output is to c: \ B .txt, it is: * PrintWriter printwriter = new PrintWriter (new FileWriter ("c: \ B .txt", true); * Now write the respond header, write it as: * PrintWriter printwriter = new PrintWriter (response. getOutputStream (); * change the output point of PrintWriter */PrintWriter printwriter = new PrintWriter (response. getOutputStream (); printwriter. println ("haha! "); // Print all the output content of the stream. The print stream must be closed for printwriter. close () ;}} to be valid ();}}