Java Download File sample

Source: Internet
Author: User

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

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.