/**
 * 
 */
package com.ssj.sys.util.thirdparty.http;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @author Goofy 
 * Http下载工具类
 */
public class HttpServletUtils {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	/**
	 * 获取项目网络路径
	 * @param request
	 * @return
	 */
	public static String getContentpath(HttpServletRequest request){
		return request.getContextPath();
	}
	
	/**
     * 获取项目磁盘绝对路径
     */
    public static String getRealPath(HttpServletRequest request){
        return request.getSession().getServletContext().getRealPath("/");
    }
    
    /**
     * 使用了代理服务器的，无法获取正确地址的，使用这个方法获取访问者的IP地址
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

	/**
	 * 下载多个文件
	 * 
	 * @param response
	 *            HttpServletResponse
	 * @param files
	 *            File
	 */
	public void download(HttpServletResponse response, File... files) {

		for (File file : files) {
			download(response, file, file.getName());
		}

	}

	/**
	 * 下载文件
	 * 
	 * @param response
	 *            HttpServletResponse
	 * @param file
	 *            文件
	 * @param fileName
	 *            下载的输出文件名
	 */
	public void download(HttpServletResponse response, File file, String fileName) {
		InputStream is;
		String _fileName;
		try {
			is = new FileInputStream(file);
			_fileName = fileName == null ? file.getName() : fileName;
			download(response, is, _fileName);
		} catch (FileNotFoundException e) {
			logger.error("fileName=[{}]", fileName);
			logger.error("下载异常", e);
		}
	}

	/**
	 * 下载文件
	 * 
	 * @param is
	 *            输入流
	 * @param fileName
	 *            文件名
	 * @param response
	 */
	public void download(HttpServletResponse response, InputStream is, String fileName) {

		OutputStream os = null;

		try {
			response.reset();
			response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

			os = response.getOutputStream();

			byte buffer[] = new byte[1024];
			int len = 0;

			while ((len = is.read(buffer)) > 0) {
				os.write(buffer, 0, len);
			}

			os.flush();
			os.close();

			is.close();

		} catch (Exception e) {
			try {
				if (os != null) {
					os.close();
				}
			} catch (IOException e1) {
				logger.error("异常", e1);
			}
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e1) {
				logger.error("异常", e1);
			}
		}

	}

}
