1.getOutputStream () have already been called for this response
Reasons for the error:
Only binary files can be written using the Tomcat container when invoking the Response.getoutputstream () method.
But when you call the Requonse.getwriter () method, you can only print text files in the foreground.
So:
If you are on a JSP page:
Outputstream.flush (); Outputstream.close (); Outputstream=null;response.flushbuffer (); Out.clear (); out = Pagecontext.pushbody ();
Here out and PageContext are the built-in objects of JSP;
If in the background:
If you use Response.getwrter, you can only use Response.setcontenttype ("text/html") in front;
If you want to output binary streams, then it is not enough to use response.setcontenttype ("text/html");
If you want to transfer files, it is recommended that:
Response.setcontenttype ("Application/x-msdownload;charset=utf-8");
At the same time in order to prevent garbled, response.setcharacterencoding ("Utf-8");
filename = urlencoder.encode (filename, "utf-8");
Code for file Download:
public void Getdownloadbook () throws Ioexception{response.setcharacterencoding ("Utf-8"); String filepath=request.getsession (). Getservletcontext (). Getrealpath ("\ \") + "upload\\books\\";//through the foreground bookid, Remove the file from the database save Information Book Book=this.getbookservice (). GetBook (BookId), if (book==null) {Response.setcontenttype ("text/html "); PrintWriter writer=response.getwriter (); Writer.write ("
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
J2ee,jsp,servlet File Download server side