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 code 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 the path to the FilePath file on the server, including the 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 the path to the FilePath file on the server, including the file name
* @param filename File to the name of the browser, if you do not want the browser to download the same file name as the file name on the server, set this parameter
*/
public static void Download (HttpServletResponse response, String FilePath, String fileName) {
File File = new file (filepath.tostring ());
Download (response, file, FileName, encoding);
}
/**
* File Download
* @param response
* @param the path to the FilePath file on the server, including the file name
* @param filename File to the name of the browser, if you do not want the browser to download the same file name as the file name on the server, set this parameter
* @param encoding file name encoding
*/
public static void Download (HttpServletResponse response, String FilePath, String fileName, string encoding) {
File File = new file (filepath.tostring ());
Download (response, file, FileName, encoding);
}
/**
* File Download
* @param response
* @param file files
* @param filename File to the name of the browser, if you do not want the browser to download the same file name as the file name on the server, set this parameter
*/
public static void Download (httpservletresponse response, file file) {
Download (response, file, null, encoding);
}
/**
* File Download
* @param response
* @param file files
* @param filename File to the name of the browser, if you do not want the browser to download the same file name as the file name on the server, set this parameter
*/
public static void Download (httpservletresponse response, file file, String fileName) {
Download (response, file, FileName, encoding);
}
/**
* File Download
* @param response
* @param file files
* @param filename File to the name of the browser, if you do not want the browser to download the same file name as the file name on the server, set this parameter
* @param encoding file name encoding
*/
public static void Download (httpservletresponse response, file file, string fileName, string encoding) {
if (file = = NULL | |!file.exists () | | file.isdirectory ()) {
Return
}
If you do not specify a file 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);
Download (response, is, fileName, encoding);
} catch (IOException e) {
E.printstacktrace ();
}
}
/**
* File Download
* @param response
* @param is file input stream
* @param filename Download file name
* @throws IOException
*/
public static void Download (HttpServletResponse response, InputStream is, String fileName) {
Download (response, is, fileName, encoding);
}
/**
* File Download
* @param response
* @param is file input stream
* @param filename Download file name
* @param encoding encoding format
*/
public static void Download (HttpServletResponse response, InputStream is, string fileName, string encoding) {
if (is = = 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;
}
}
Java Download File sample