下载导入数据模板

// 类:BizCommonController.java

/*
 * Copyright (C), 2002-2013, xxx电子商务有限公司
 * FileName: BizCommonController.java
 * Author:   13074110
 * Date:     2013-8-16 下午5:06:02
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.ebuy.hyj.treaty.admin.web.controller;
  
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


/**
 * @author 13074110
 * @see [相关类/方法](可选)
 * @since [产品/模块版本] (可选)
 */

@Controller
@RequestMapping("/bizCommon")
public class BizCommonController{
    
    /**
     * 下载导入数据模板
     * 
     * @param fileName
     */
    @RequestMapping(value="/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response,HttpServletRequest request,
            @RequestParam(value="fileName",required=true) String fileName){
        
        // 存放本地服务器目录
        String localServerPath = null; 
        // 存放本地文件
        File loc = null;
        // 存放文件字节流
        FileInputStream input = null;  
        // 输入缓冲流
        BufferedInputStream  bis = null;
        ByteArrayOutputStream baos = null;
        try {
            // 获取服务器目录
            localServerPath = request.getSession().getServletContext().getRealPath("/importTemplate");
            // 获取模板文件
            loc = FileUtils.getFile(localServerPath, fileName+".csv"); 
            // 将文件转化成字节流
            input = new FileInputStream(loc);
            
            // 输入缓冲流
            bis = new BufferedInputStream(input);
            // 输出缓冲流
            baos = new ByteArrayOutputStream();
            // 字节数组
            byte[] tmp = new byte[2048];
            
            int size = 0;
            while( (size = bis.read(tmp)) > 0){
                baos.write(tmp, 0, size);
            }
            baos.flush();
            
            // 将字节数组输出流转化成字符串
            String fileContent = baos.toString("GBK");
            // 弹出下载框
            CsvWriter.exportCsv(fileName, fileContent, response); 
            
        } catch(IOException e){
        	super.logException(logger, e);
        } catch (Exception e) {
        	super.logException(logger, e);
        } finally{
        	if(baos!=null){
        		try {
					baos.close();
				} catch (IOException e) {
					super.logException(logger, e);
				}
        	}
        	if(bis!=null){
        		try {
					bis.close();
				} catch (IOException e) {
					super.logException(logger, e);
				}
        	}
     
   	if(input!=null){
        		try {
					input.close();
				} catch (IOException e) {
					super.logException(logger, e);
				}
        	}
        }
          
    } 
    
}



// 类:CsvWriter.java
/*
 * Copyright (C), 2002-2013, xxx电子商务有限公司
 * FileName: CsvWriter.java
 * Author:   13074110
 * Date:     2013-8-31 下午3:12:09
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.ebuy.hyj.treaty.admin.util;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

/**
 * 导出CSV文件工具类
 * 
 * @author 13074110
 */

public class CsvWriter {

	public static void exportCsv(String fileName, String content,
			HttpServletResponse response) throws IOException {

		// 设置文件后缀
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhh24mmss");
		String fn = fileName.concat(sdf.format(new Date()).toString() + ".csv");

		// 读取字符编码
		String csvEncoding = "utf-8";

		// 设置响应
		response.setCharacterEncoding(csvEncoding);
		response.setContentType("text/csv; charset=" + csvEncoding);
		response.setHeader("Pragma", "public");
		response.setHeader("Cache-Control", "max-age=30");
		response.setHeader("Content-Disposition", "attachment; filename="
				+ new String(fn.getBytes(), csvEncoding));

		// 写出响应
		OutputStream os = response.getOutputStream();
		os.write(content.getBytes("GBK"));
		os.flush();
		os.close();
	}

}


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页