I. Using requestdispatcher
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 it to download application/X-Download
String filenamedownload = "/system resolution .doc"; // relative path of the file to be downloaded
String filenamedisplay = "system resolution .doc"; // The name of the file to be saved when the file is downloaded
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
{
}
%>
Ii. Download using file stream output
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" %> <%
// When downloading an object, use the file stream output method:
// Add response. Reset (), and do not wrap all %>, including the last one;
// Because the content between %> and <% is usually output as is when the application server processes the JSP compilation, and the default value is printwriter,
// You Need to output the stream: servletoutputstream. This is equivalent to trying to use two output mechanisms in servlet,
// The following error occurs: getoutputstream () has already been called for this response.
// For details, see the second part of "more java pitfill" web layer item 33: trying to use two output mechanisms in servlet 270
// If there is a line feed, there is no problem with text files, but for other formats, such as AutoCAD, Word, Excel and other files
// Some linefeeds 0x0d and 0x0a will be added to the downloaded file. This may cause some files in some formats to fail to be opened, and some files can be opened normally.
Response. Reset (); // either or not
Response. setcontenttype ("application/X-download"); // set it to download application/X-Download
///.. /.. /Back To The WEB-INF/classes two levels to the application root directory, pay attention to Tomcat and WebLogic below this sentence to get the path is different, Weblogic in the path does not last/
System. Out. println (this. getclass (). getclassloader (). getresource ("/"). getpath ());
String filenamedownload = This. getclass (). getclassloader (). getresource ("/"). getpath () + "/.../system resolution .doc ";
String filenamedisplay = "system resolution .doc"; // system resolution .txt
Filenamedisplay = urlencoder. encode (filenamedisplay, "UTF-8 ");
Response. addheader ("content-disposition", "attachment; filename =" + filenamedisplay );
Outputstream output = NULL;
Fileinputstream FCM = NULL;
Try
{
Output = response. getoutputstream ();
FS = new fileinputstream (filenamedownload );
Byte [] B = new byte [1024];
Int I = 0;
While (I = FCM. Read (B)> 0)
{
Output. Write (B, 0, I );
}
Output. Flush ();
}
Catch (exception E)
{
System. Out. println ("error! ");
E. printstacktrace ();
}
Finally
{
If (FS! = NULL)
{
FCM. Close ();
FS = NULL;
}
If (output! = NULL)
{
Output. Close ();
Output = NULL;
}
}
%>