I encountered a problem in the jsp download function. I don't know why:
First, I made a jsp file: filedown. jsp
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> hello </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8">
</Head>
<Body>
<A href = "DownloadServlet"> download </a>
</Body>
</Html>
Then I made a servlet: DownloadServlet. java
Import java. io. BufferedInputStream;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. IOException;
Import java. io. OutputStream;
Import java. io. PrintWriter;
Import javax. servlet. ServletException;
Import javax. servlet. ServletOutputStream;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class DownloadServlet extends HttpServlet {
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @ Param request the request send by the client to the server
* @ Param response the response send by the server to the client
* @ Throws ServletException if an error occurred
* @ Throws IOException if an error occurred
*/
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
String filePath = "D:/apple/2007.doc ";
String fileType = "doc ";
File file = new File (filePath );
Response. setCharacterEncoding ("gb2312 ");
Response. setContentType (fileType );
Response. setHeader ("Content-Disposition", "attachment; filename =" + new String (file. getName (). getBytes ("gb2312"), "ISO8859-1 "));
System. out. println (new String (file. getName (). getBytes ("gb2312"), "gb2312 "));
OutputStream output = null;
FileInputStream FCM = null;
Try
{
Output = response. getOutputStream ();
FS = new FileInputStream (file );
Byte [] B = new byte [1024];
Int I = 0;
System. out. println ("++ ");
While (I = FI. read (B ))! =-1)
{
Output. write (B, 0, I );
}
Output. write (B, 0, B. length );
System. out. println ("++ ");
Output. flush ();
Response. flushBuffer ();
}
Catch (Exception e)
{
System. out. println ("Error! ");
E. printStackTrace ();
}
Finally
{
If (FS! = Null)
{
FCM. close ();
FS = null;
}
If (output! = Null)
{
Output. close ();
Output = null;
}
}
}
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @ Param request the request send by the client to the server
* @ Param response the response send by the server to the client
* @ Throws ServletException if an error occurred
* @ Throws IOException if an error occurred
*/
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
DoGet (request, response );
}
}
The servlet above is the source code I found online. I added output. write (B, 0, B. length. (But I did not add the same effect, it is all wrong)
My preparation work is: D:/apple/2007.doc. This file must exist.