JSP file download and appear Getoutputstream () have already been called for this response workaround

Source: Internet
Author: User

JSP file download and appear Getoutputstream () have already been called for this response workaround

Http://iamin.blogdriver.com/iamin/1072546.html
First, the use of RequestDispatcher way to carry out

1. Add the Web. xml file
<mime-mapping>
<extension>doc</extension>
<mime-type>application/vnd.ms-word</mime-type>
</mime-mapping>


2, the procedure is as follows:

<% @page language= "java" import= "java.net.*" pageencoding= "gb2312"%>
<%
Response.setcontenttype ("Application/x-download");//set to download Application/x-download
String filenamedownload = "/System solution. doc";//relative path of the file that will be downloaded
String filenamedisplay = "System solution. Doc";//File Save name displayed when downloading file
Filenamedisplay = Urlencoder.encode (Filenamedisplay, "UTF-8");
Response.AddHeader ("Content-disposition", "attachment;filename=" + filenamedisplay);

Try
{
RequestDispatcher dispatcher = Application.getrequestdispatcher (filenamedownload);
if (Dispatcher! = null)
{
Dispatcher.forward (Request,response);
}
Response.flushbuffer ();
}
catch (Exception e)
{
E.printstacktrace ();
}
Finally
{

}
%>


Second, the use of file stream output of the way to download

1. Add the Web. xml file
<mime-mapping>
<extension>doc</extension>
<mime-type>application/vnd.ms-word</mime-type>
</mime-mapping>


2, the procedure is as follows:

<% @page language= "java" contenttype= "Application/x-msdownload" import= "java.io.*,java.net.*" pageEncoding= " gb2312 "%><%
File stream output is processed when downloading files:
Add Response.reset () and do not wrap all the%>, including the last one;
Because application server handles compiling the JSP, the content between%> and <% is generally output as is, and the default is PrintWriter,
And you're going to stream output: Servletoutputstream, which is equivalent to trying to use two output mechanisms in the servlet,
Will occur: Getoutputstream () has already been called for this response error
See the second section of the "more Java pitfill" Web Layer Item 33: Trying to use two output mechanisms in a servlet 270
And if there is a line break, there is no problem with the text file, but for other formats, such as AutoCAD, Word, Excel, and other files
The downloaded file will have some more line breaks 0x0d and 0x0a, which may cause some format files to not open, some can open normally.

Response.reset ();//Can be added or not added
Response.setcontenttype ("Application/x-download");//set to download Application/x-download
// /.. /.. /Retire Web-inf/classes two to the root of the application, notice that Tomcat and WebLogic have different paths from the following sentence, and the path in WebLogic does not last
System.out.println (This.getclass (). getClassLoader (). GetResource ("/"). GetPath ());
String filenamedownload = This.getclass (). getClassLoader (). GetResource ("/"). GetPath () + "/". /.. /System solutions. doc ";
String filenamedisplay = "System solution. Doc";//System solution. txt
Filenamedisplay = Urlencoder.encode (Filenamedisplay, "UTF-8");
Response.AddHeader ("Content-disposition", "attachment;filename=" + filenamedisplay);

OutputStream output = null;
FileInputStream FIS = null;
Try
{
Output = Response.getoutputstream ();
FIS = new FileInputStream (filenamedownload);

Byte[] B = new byte[1024];
int i = 0;

while ((i = Fis.read (b)) > 0)
{
Output.write (b, 0, I);
}
Output.flush ();
}
catch (Exception e)
{
System.out.println ("error!");
E.printstacktrace ();
}
Finally
{
if (FIS! = null)
{
Fis.close ();
FIS = null;
}
if (output! = NULL)
{
Output.close ();
output = null;
}
}
%>

JSP file download and appear Getoutputstream () have already been called for this response workaround

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.