package com.xmomen.module.export.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.xmomen.module.export.model.UploadFileVo;


public class PrintUtils {

	// 导出Excel文件
	public static void excelResponseOutput(HttpServletResponse response, HSSFWorkbook workbook, String outPutFileName) {
		try{
			// 重置response
			response.reset();
			// 设置文件下载头
			response.addHeader("Content-Disposition", "attachment;filename=" + outPutFileName);
			// 设置文件类型
			response.setContentType("application/msexcel-print-Surface;charset=UTF-8");
			// 获取输出流
			OutputStream out = response.getOutputStream();
			// 写入文件
			workbook.write(out);
			// 刷新输出流
			out.flush();
			// 释放资源
			workbook = null;
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 导出Excel文件
	public static void excelResponseOutput(HttpServletResponse response, XSSFWorkbook workbook, String outPutFileName) {
		try{
			// 重置response
			response.reset();
			// 设置文件下载头
			response.addHeader("Content-Disposition", "attachment;filename=" + outPutFileName);
			// 设置文件类型
			response.setContentType("application/msexcel-print-Surface;charset=UTF-8");
			// 获取输出流
			OutputStream out = response.getOutputStream();
			// 写入文件
			workbook.write(out);
			// 刷新输出流
			out.flush();
			// 释放资源
			workbook = null;
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 导出标签文件
	public static void labelResponseOutput(HttpServletResponse response, HSSFWorkbook workbook, String outPutFileName) {
		try{
			// 重置response
			response.reset();
			// 设置文件下载头
			response.addHeader("Content-Disposition", "attachment;filename=" + outPutFileName);
			// 设置文件类型
			response.setContentType("application/msexcel-print-Consignment;charset=UTF-8");
			// 获取输出流
			OutputStream out = response.getOutputStream();
			// 写入文件
			workbook.write(out);
			// 刷新输出流
			out.flush();
			// 释放资源
			workbook = null;
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 导出标签文件
	public static void labelResponseOutput(HttpServletResponse response, XSSFWorkbook workbook, String outPutFileName) {
		try{
			// 重置response
			response.reset();
			// 设置文件下载头
			response.addHeader("Content-Disposition", "attachment;filename=" + outPutFileName);
			// 设置文件类型
			response.setContentType("application/msexcel-print-Consignment;charset=UTF-8");
			// 获取输出流
			OutputStream out = response.getOutputStream();
			// 写入文件
			workbook.write(out);
			// 刷新输出流
			out.flush();
			// 释放资源
			workbook = null;
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

	@SuppressWarnings("unchecked")
	public static HttpServletResponse viewOrDownloadFile(UploadFileVo uploadFile) {
		// 设置response的编码
		uploadFile.getResponse().setContentType("UTF-8");
		uploadFile.getResponse().setCharacterEncoding("UTF-8");
		// 获取输入流
		InputStream bis = null;
		// 获取输出流
		BufferedOutputStream bos = null;
		// 获取response
		HttpServletResponse response = uploadFile.getResponse();
		// 获取request
		HttpServletRequest request = uploadFile.getRequest();
		// 获取项目根路径
		String ctxPath = request.getSession().getServletContext().getRealPath("/");
		// 下载路径
		String downLoadPath = "";
		// 文件长度
		long fileLength = 0;
		// 如果有真实路径
		if (uploadFile.getRealPath() != null&&uploadFile.getContent() == null) {
			// 设置下载路径
			downLoadPath = ctxPath + uploadFile.getRealPath();
			// 获取文件长度
			fileLength = new File(downLoadPath).length();
			try {
				// 获取输入流
				bis = new BufferedInputStream(new FileInputStream(downLoadPath));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		} else {
			// 如果有文件内容
			if (uploadFile.getContent() != null)
				// 获取输入流
				bis = new ByteArrayInputStream(uploadFile.getContent());
			// 获取文件长度
			fileLength = uploadFile.getContent().length;
		}
		try {
			// 如果不是预览，并且有扩展名
			if (!uploadFile.isView() && uploadFile.getExtend() != null) {
				// 根据扩展名设置文件类型
				if (uploadFile.getExtend().equals("text")) {
					response.setContentType("text/plain;");
				} else if (uploadFile.getExtend().equals("doc")) {
					response.setContentType("application/msword;");
				} else if (uploadFile.getExtend().equals("xls")|| uploadFile.getExtend().equals("xlsx")) {
					response.setContentType("application/ms-excel;");
				} else if (uploadFile.getExtend().equals("pdf")) {
					response.setContentType("application/pdf;");
				} else if (uploadFile.getExtend().equals("jpg") || uploadFile.getExtend().equals("jpeg")) {
					response.setContentType("image/jpeg;");
				} else {
					response.setContentType("application/x-msdownload;");
				}
				// 设置文件下载头
				response.setHeader("Content-disposition", "attachment; filename=" + new String((uploadFile.getTitleField() + "." + uploadFile.getExtend()).getBytes("GBK"), "ISO8859-1"));
				// 设置文件长度
				response.setHeader("Content-Length", String.valueOf(fileLength));
			}
			// 获取输出流
			bos = new BufferedOutputStream(response.getOutputStream());
			// 定义缓冲区
			byte[] buff = new byte[2048];
			// 定义读取的字节数
			int bytesRead;
			// 循环读取文件内容
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				// 写入输出流
				bos.write(buff, 0, bytesRead);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 关闭输入流
				if (bis != null) {
					bis.close();
				}
				// 关闭输出流
				if (bos != null) {
					bos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return response;
	}
}
