Jsp stream-based Download

Source: Internet
Author: User

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.

Related Article

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.