Servle File Download

Source: Internet
Author: User
Package com. Future. ZFS. util;

Import java. Io. bufferedinputstream;
Import java. Io. bufferedoutputstream;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. outputstream;

Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;

@ Suppresswarnings ("serial ")
Public class filedownloadservlet extends httpservlet {

@ Override
Protected void Service (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
String Path = request. getparameter ("FILENAME ");
Path = new string (path. getbytes ("ISO-8859-1"), "UTF-8 ");
Download (path, request, response );
}

@ Suppresswarnings ("deprecation ")
Public httpservletresponse download (string path, httpservletrequest request, httpservletresponse response ){
Try {
// Path indicates the path of the object to be downloaded.
File file = new file (request. getrealpath ("/") + "/" + path );
// Obtain the file name.
String filename = file. getname ();
// Get the suffix of the file.
// String ext = filename. substring (filename. lastindexof (".") + 1). touppercase ();

// Download an object as a stream.
Inputstream FCM = new bufferedinputstream (New fileinputstream (File ));
Byte [] buffer = new byte [FCM. Available ()];
FS. Read (buffer );
FCM. Close ();
// Clear response
Response. Reset ();
// Set the Response Header
Response. addheader ("content-disposition", "attachment; filename =" + new string (filename. getbytes ("UTF-8"), "ISO-8859-1 "));
Response. addheader ("Content-Length", "" + file. Length ());
Outputstream toclient = new bufferedoutputstream (response. getoutputstream ());
Response. setcontenttype ("application/octet-stream ");
Toclient. Write (buffer );
Toclient. Flush ();
Toclient. Close ();
} Catch (ioexception ex ){
Ex. printstacktrace ();
}
Return response;
}
}

Web. xml
<Servlet>
<Servlet-Name> filedownloadservlet </servlet-Name>
<Servlet-class> com. Future. ZFS. util. filedownloadservlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> filedownloadservlet </servlet-Name>
<URL-pattern>/filedownloadservlet </url-pattern>
</Servlet-mapping> <a href = "filedownloadservlet? Filename= .xls "> Haha, Test File Download </a>

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.