com.mmj.app.common.interceptor.ExportFileAnnotationInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for com.mmj.app.common.interceptor.ExportFileAnnotationInterceptor.java

Source

/*
 * Copyright 2011-2016 MSUN.com All right reserved. This software is the confidential and proprietary information of
 * MSUN.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with MSUN.com.
 */
package com.mmj.app.common.interceptor;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.mmj.app.common.file.ExcelUtils;
import com.mmj.app.common.interceptor.annotation.ExportExcelFile;
import com.mmj.app.common.interceptor.annotation.ExportWordFile;

/**
 * @author zxc Aug 7, 2014 11:26:48 PM
 */
public class ExportFileAnnotationInterceptor extends HandlerInterceptorAdapter {

    public static Logger logger = LoggerFactory.getLogger(ExportFileAnnotationInterceptor.class);

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        if (handler == null || response == null) {
            return;
        }

        HandlerMethod hm = (HandlerMethod) handler;

        ExportWordFile exportWordFile = hm.getMethodAnnotation(ExportWordFile.class);
        // word,
        if (exportWordFile != null) {
            String wordName = exportWordFile.value();
            if (StringUtils.isEmpty(wordName)) {
                return;
            }
            String contentDis = "attachment;filename=" + wordName + ".doc";
            response.setHeader("content-disposition", contentDis);
            response.setContentType("application/msword;");
            response.setCharacterEncoding("UTF-8");
        }

        ExportExcelFile exportExcelFile = hm.getMethodAnnotation(ExportExcelFile.class);
        // excel,
        if (exportExcelFile != null) {
            String xlsName = exportExcelFile.value();
            if (StringUtils.isEmpty(xlsName)) {
                return;
            }
            List<?> list = (List<?>) modelAndView.getModel().get("list");
            String[] head = (String[]) modelAndView.getModel().get("head");
            modelAndView.clear();

            HSSFWorkbook workbook = ExcelUtils.defBuildExcel(list, xlsName, head);

            if (workbook == null) {
                try {
                    response.getOutputStream().print("Not conform to the requirements of data");
                } catch (IOException e) {
                    logger.error(e.getMessage(), e);
                }
                return;
            }

            response.setHeader("content-disposition", "attachment;filename=" + xlsName + ".xls");
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("UTF-8");
            //  response
            OutputStream os = response.getOutputStream();
            workbook.write(os);
            os.flush();
            os.close();
        }
    }
}