This article shares Spring 4 download components for your reference. The specific content is as follows
package com.hnust.common.controller; import org.apache.commons.io.FileUtils;import org.springframework.http.HttpHeaders;import org.springframework.http.HttpStatus;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.RestController;import java.io.File;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.net.URLEncoder; /** * Created by Heweipo on 2016/5/27. * <p> * Download general controller*/@RestControllerpublic class DownloadController extends BaseController { /** * Download file general method* * @param file file object* @return file byte stream*/ public ResponseEntity<byte[]> export(File file) { return export(file.getName(), file); } /** * General method for downloading files* * @param fileName File name* @param file file object* @return File byte stream*/ public ResponseEntity<byte[]> export(String fileName, File file) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", encodeFileName(fileName)); ResponseEntity<byte[]> rs = null; try { // HttpStatus.CREATED 201 cannot be used here because IE Edge cannot recognize it, but Firefox chrome is no problem rs = new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK); } catch (IOException e) { //throw new CommonException(ResponseStatusEnum.FILE_ERROR, e); } return rs; } /** * The name of the download file, which is the name displayed in the browser* * @param fileName File name* @return Added file name* <p> * IE * Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko * <p> * Edge * Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586 * <p> * Firefox * Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0 * <p> * Chrome * Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36 */ private String encodeFileName(String fileName) { String name = fileName; try { String agent = request.getHeader("USER-AGENT").toLowerCase(); if (null != agent && (agent.contains("msie") || agent.contains("edge"))) { // IE edge name = URLEncoder.encode(fileName, "UTF-8"); } else if (agent.contains("safari") || agent.contains("chrome") || agent.contains("firefox")) { // safari chrome firefox name = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); } else { // IE10 IE11 name = URLEncoder.encode(fileName, "UTF-8"); } // Restore the plus sign to a space (IE Edge has a problem) name = name.replace("+", "%20"); } catch (UnsupportedEncodingException e) { // throw new CommonException(ResponseStatusEnum.FAILURE, e); } return name; } } The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.