1. 程式人生 > 實用技巧 >HTTP下載Excel工具類

HTTP下載Excel工具類

使用示例:

   @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ResponseEntity<byte[]> downloadPMetricRecords(
            @PathVariable(value = "resource_type") ResourceTypeEnum resourceType,
            @RequestParam(value = "pool_id", required = false) String poolId,
            @RequestParam(value = "business_id", required = false) String businessId,
            @RequestParam(value = "organization_id", required = false) String organizationId,
            @RequestParam(value = "start_time", required = false) /*@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")*/ Date starTime,
            @RequestParam(value = "end_time", required = false) /*@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") */Date endTime,
            @RequestParam(value = "export_format", required = false) DocFormat exportFormat,
            @RequestParam(value = "statistic_type", required = false) StatisticType statisticType) throws FileNotFoundException {
        PMetricExportFilterParam exportFilterParam = PMetricExportFilterParam.builder()
                .resourceType(resourceType)
                .poolId(poolId)
                .businessId(businessId)
                .organizationId(organizationId)
                .startTime(starTime)
                .endTime(endTime)
                .exportFormat(exportFormat)
                .statisticType(statisticType)
                .build();
        logger.info("PMetricExportController(downloadPMetricRecords) - query param: {}", exportFilterParam);
        initDefaultValues(exportFilterParam);
        validate(exportFilterParam);
        IPMetricExportService metricExportService = metricExportServiceFactory.createMetricExportService(exportFilterParam);
        return metricExportService.downloadPMetricRecords(exportFilterParam);
    }

其他工具類:

package com.chinamobile.epic.tako.common.download;
import com.ssslinppp.write.AbstractWriteHandleFactory;
import com.ssslinppp.write.WriteHandler;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Desc:
 * [large files for download, OutOfMemoryException](https://stackoverflow
 * .com/questions/15800565/spring-mvc-large-files-for-download-outofmemoryexception)
 * <p>
 */
@Component
public class HttpDownloadUtils {
    private static final Logger logger = LoggerFactory.getLogger(HttpDownloadUtils.class);
    private final static String EXCEL_2007_SUFFIX = ".xlsx";
    @Value("${download.tmp.file.path: /tmp/}")
    private String tmpFilePath;
    /**
     * HTTP下載Excel
     * <p>
     * 使用示例:</br>
     * <blockquote><pre>{@code
     * Map<String, List<PMetricDimensionTBL>> recordMap = Maps.newConcurrentMap();
     * recordMap.put("sheetName001", records);
     * recordMap.put("sheetName002", records);
     * <p>
     * String excelFileName = "pmetric-report.xlsx";
     * httpDownloadUtils.httpDownloadExcel(new AbstractWriteHandleFactory<PMetricDimensionTBL>() {
     * }, recordMap, excelFileName);
     * }
     * </pre></blockquote>
     *
     * @param writeHandleFactory 用於寫Excel,如:{@code new AbstractWriteHandleFactory<PMetricDimensionTBL>() {
     *                           }}
     * @param recordsMap         {@code Map<sheetName, records>}  待寫入Excel檔案的記錄
     * @param targetFileName     目標檔名
     * @param <T>                屬性中應該宣告{@link com.ssslinppp.annotation.ExcelCell @ExcelCell}
     * @return
     * @throws FileNotFoundException
     */
    public <T> ResponseEntity<byte[]> httpDownloadExcel(AbstractWriteHandleFactory<T> writeHandleFactory,
                                                        Map<String, List<T>> recordsMap,
                                                        String targetFileName) throws FileNotFoundException {
        // 寫入臨時檔案
        String tmpFileToUse = tmpFilePath + UUID.randomUUID() + EXCEL_2007_SUFFIX;
        WriteHandler writeHandler = writeHandleFactory.createWriteHandle(tmpFileToUse);
        for (Map.Entry<String, List<T>> entry : recordsMap.entrySet()) {
            writeHandler.write(entry.getKey(), entry.getValue());
        }
        writeHandler.flush();
        // 將臨時檔案轉換為 byte[]
        byte[] fileBytes = null;
        try {
            InputStream in = new FileInputStream(tmpFileToUse);
            fileBytes = IOUtils.toByteArray(in);
        } catch (IOException e) {
            logger.error("HttpDownloadUtils(exportExcel) - get byte[] from " + tmpFileToUse + " error", e);
        } finally {
            File file = new File(tmpFileToUse);
            if (file.exists()) {
                logger.info("HttpDownloadUtils(exportExcel) - delete tmp file {}", tmpFileToUse);
                file.delete();
            }
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); //檔案下載需要設定: application/octet-stream
        headers.setContentDispositionFormData("attachment", targetFileName); // 設定下載的檔名稱
        return new ResponseEntity<byte[]>(fileBytes, headers, HttpStatus.CREATED);
    }
}