There are two kinds of web file download, one is file in the website directory, in the browser directly enter the file path can be downloaded, such as Http://www.xxx.com/file.zip. The other is that the file is not in the site directory or the file is generated dynamically (export reports or export Excel, etc.), this situation requires the response OutputStream implementation of the file download. Downloadutils is a Java web file Download tool class that provides a variety of static methods for file download.
Package Com.rhui.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 Java.net.urlencoder;import Javax.servlet.http.httpservletresponse;import org.apache.commons.lang3.stringutils;/** * File Download class */public class Downloadutils {/** * File download code * This encoding tells the browser file name encoding method, In case the download Chinese file name is garbled */private static String encoding = "Utf-8";/** * File Download * @param response * @param filePath file on the server path, including file name * /public static void Download (HttpServletResponse response, String filePath) {File File = new file (filepath.tostring ()); Download (response, file, null, encoding);} /** * File Download * @param response * @param filePath file on the server path, including file name * @param fileName file to download the name of the browser, if you do not want the browser to download the file name and the file name on the server , set the parameter */public static void download (HttpServletResponse response, String FilePath, String fileName) {File File = new file (Filepath.tostring ());d ownload (response, file, FileName,encoding);} /** * File Download * @param response * @param filePath file on the server path, including file name * @param fileName file to download the name of the browser, if you do not want the browser to download the file name and the file name on the server , set the parameter * @param encoding file name encoding */public static void Download (HttpServletResponse response, String FilePath, String filen AME, String encoding) {File File = new file (filepath.tostring ());d ownload (response, File, fileName, encoding);} /** * File Download * @param response * @param file files * @param fileName file download to the browser name, if you do not want the browser to download the file name and the name of the file on the server, please set this parameter */public St atic void Download (httpservletresponse response, file file) {Download (response, file, null, encoding);} /** * File Download * @param response * @param file files * @param fileName file download to the browser name, if you do not want the browser to download the file name and the name of the file on the server, please set this parameter */public St atic void Download (httpservletresponse response, file file, String fileName) {Download (response, File, FileName, encoding );} /** * File Download * @param response * @param file files * @param fileName file download to the browser name, if you do not want the browser to download the file name and the file name on the server, set this parameter * @param en Coding file name encoding */public static void DownloaD (httpservletresponse response, file file, string fileName, string encoding) {if (File = = NULL | |!file.exists () | | file.is Directory ()) {return;} If you do not specify a file to download to the browser name, the default name of the file is used if (Stringutils.isblank (filename)) {filename = File.getname ();} try {InputStream is = new FileInputStream (file);d ownload (response, is, fileName, encoding);} catch (IOException e) {E.prin Tstacktrace ();}} /** * File Download * @param response * @param is file input stream * @param filename downloaded file name * @throws ioexception */public static void Downlo AD (httpservletresponse response, InputStream is, String fileName) {Download (response, is, fileName, encoding);} /** * File Download * @param response * @param is file input stream * @param filename downloaded file name * @param encoding encoded format */public static void downl Oad (httpservletresponse response, InputStream is, string fileName, string encoding) {if (= = NULL | | Stringutils.isblank (fileName) {return;} Bufferedinputstream bis = null;outputstream OS = null; Bufferedoutputstream BOS = Null;try{bis = new Bufferedinputstream (is); OS = ResPonse.getoutputstream (); bos = new Bufferedoutputstream (OS); Response.setcontenttype ("Application/octet-stream; charset= "+ encoding); response.setcharacterencoding (encoding); Response.setheader ("Content-disposition", "attachment;filename=" + urlencoder.encode (filename, encoding)); byte[] buffer = new byte[1024]; int len = bis.read (buffer); while (len! =-1) {bos.write (buffer, 0, Len); Len = bis.read (buffer); } bos.flush ();} catch (IOException e) {e.printstacktrace ();} Finally{if (bis! = null) {Try{bis.close ();} catch (IOException e) {}}if (is! = null) {Try{is.close ();} catch (IOException e) {}}}}public static String getencoding () {return encoding;} public static void Setencoding (String encoding) {downloadutils.encoding = encoding;}}
If the file is saved in a non-site directory on the server
String FilePath = "C:\\file.zip";D ownloadutils.download (response, FilePath);
If the file is an input stream
is the file input stream//filename for the browser download the file name//encoding for the file name encoding, prevent the file in the Chinese language when generated garbled string fileName = "File.zip"; String encoding = "Utf-8";D ownloadutils.download (response, is, fileName, encoding);
File Download in servlet
Package Com.rhui.web.servlet;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.annotation.webservlet;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.rhui.util.DownloadUtils, @WebServlet ("/download/servlet") public class Downloadservlet extends HttpServlet { Private static final Long serialversionuid = 1l;protected void Service (HttpServletRequest request, HttpServletResponse re Sponse) throws Servletexception, IOException {String FilePath = "C:\\file.zip";D ownloadutils.download (response, FilePath);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Web File download