Server-side implementation file Download function code

Source: Internet
Author: User

Package com.cn;import java.io.bufferedinputstream;import java.io.bufferedoutputstream;import  java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import  java.io.outputstream;import java.net.urlencoder;import javax.servlet.servletexception;import  javax.servlet.annotation.webservlet;import javax.servlet.http.httpservlet;import  javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** *  servlet implementation class download */@WebServlet ("/download") public class  Download extends httpservlet {    private static final long  serialVersionUID = 1L;    /**     *  @see  httpservlet#httpservlet ()      */    public download ()  {       &nbSp;super ();         // todo auto-generated constructor  stub    }    /**     *  @see   Httpservlet#doget (httpservletrequest request, httpservletresponse     *       response)      */    protected  void doget (httpservletrequest request,             httpservletresponse response)  throws ServletException, IOException  {        string name =  "aaa.*";//  file name          string uploadpath = uploadfilehelper.getrepositorypath ()  +  "//";//  File source         string filepath =  name;        string filename = name;           //processing file name contains Chinese, browser garbled         if  ( Request.getheader ("User-agent"). toLowerCase (). IndexOf ("Firefox")  > 0)  {             filename = new string (FileName.getBytes (" UTF-8 "), " iso8859-1 ");// firefox browser         } else  {            if  (Request.getHeader (" User-agent "). toUpperCase (). IndexOf (" MSIE ")  > 0)  {                 filename = urlencoder.encode (fileName,  " UTF-8 ");// ie browser             }         } &nBsp;      response.setcontenttype ("Text/plain");         response.setheader ("Location",  filename);         response.reset ();         response.setheader ("Cache-Control",   "max-age=0");         response.setheader ("Content-Disposition",   "Attachment; filename="                  + filename);         bufferedinputstream  bis = null;        BufferedOutputStream bos  = null;        outputstream fos = null;         InputStream fis = null;         filepath = uploadpath + filepath;        fis = new  fileinputstream (FilePath);        bis = new  Bufferedinputstream (FIS);        fos =  Response.getoutputstream ();        bos = new  Bufferedoutputstream (FOS);        int bytesread = 0;         byte[] buffer = new byte[5 *  1024];        while  ((bytesread = bis.read (buffer))  != -1)  {            bos.write ( Buffer, 0, bytesread);//  send the file to the client         }         bos.close ();          bis.close ();         fos.close ();         fis.close ();     }    /**      *  @see  httpservlet#dopost (httpservletrequest request,  Httpservletresponse     *      response)       */    protected void dopost (HttpServletRequest request,             httpservletresponse response)  throws ServletException, IOException {         Doget (request, response);     }}


This article is from the "Nothing-skywalker" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1694479

Server-side implementation file Download function code

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.