Java Web file download

Source: Internet
Author: User

Java Web file download

There are two types of Web file downloads: one is to download files in the website directory, directly enter the file path in the browser, such as http://www.xxx.com/file.zip. In addition, files are not in the website directory or are dynamically generated (export reports or excel files). In this case, you need to use the OutputStream of response to download files. DownloadUtils is a Java Web file download tool class that provides multiple 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 the encoding method of the file name to prevent garbled characters when downloading the Chinese file name */private static String encoding = UTF-8; /*** File download ** @ param response * @ param filePath the file path 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 filePath path of the file on the server, including the file name * @ param fileName: the name of the file downloaded to the browser. The file name is the same 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 filePath path of the file on the server, including the file name * @ param fileName: the name of the file downloaded to the browser. If you do not want the file to be downloaded by the browser to be the same as the file name on the server, set this parameter * @ param encoding file name encoding */public static void download (HttpSer VletResponse response, String filePath, String fileName, String encoding) {File file = new File (filePath. toString (); download (response, file, fileName, encoding );} /*** file Download ** @ param response * @ param file * @ param fileName the name of the file downloaded to the browser, if you do not want the File name downloaded by the browser to be the same 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 * @ param fileName indicates the name of the file downloaded to the browser. If you do not want the name of the file downloaded by the browser to be the same as that of the file 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 * @ param fileName the name of the file downloaded to the browser, if you do not want the file name downloaded by the browser to be the same as the file name on the server, set this parameter * @ param encoding file name encoding */public static void download (H TtpServletResponse response, File file, String fileName, String encoding) {if (file = null |! File. exists () | file. isDirectory () {return;} // if the name of the file downloaded to the browser is not specified, the default object name 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 name of the downloaded file * @ 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 file name * @ param 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 [10 24]; 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 stored in a non-website directory on the server

 

 

String filePath = c:\file.zip;DownloadUtils.download(response, filePath);

 

 

If the file is an input stream

 

// Is the file input stream // fileName is the name of the file downloaded by the browser // encoding is the file name encoding to prevent garbled String fileName = file.zip when the file contains Chinese characters; string encoding = UTF-8; DownloadUtils. download (response, is, fileName, encoding );

Servlet File Download

 

 

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 response) throws ServletException, IOException {String filePath = c:\file.zip;DownloadUtils.download(response, filePath);}}


 

Related Article

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.