/**
 * Company Name : 中贸促信息技术有限责任公司
 * Project Name:memberManageSys
 * File Name:DownloadFileController.java
 * Package Name:ccpit.base.controller
 * Date:2016年3月3日下午1:57:15
 * Copyright (c) 2016, dingwei@ccpit.org All Rights Reserved.
 *
*/

package org.ccpit.base.controller;

import java.net.HttpURLConnection;
import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * ClassName:DownloadFileController <br/>
 * Function: 导入模板下载控制类. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2016年3月3日 下午1:57:15 <br/>
 * @author   dingwei
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Controller
@RequestMapping("base/download")
public class DownloadFileController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(DownloadFileController.class);
	
	
/*	@RequestMapping("/downloadFile")
	@ResponseBody
	public Object downloadFile(HttpServletRequest request, HttpServletResponse response){
		Map result = new HashMap();
		//获得请求文件名  
        String filename = request.getParameter("filename");
        String fileRealName = request.getParameter("fileRealName");
        try {
        	fileRealName = java.net.URLDecoder.decode(fileRealName, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
        //设置文件MIME类型  
        response.setContentType(request.getSession().getServletContext().getMimeType(filename));  
        //设置Content-Disposition  
        try {
			response.setHeader("Content-Disposition", "attachment;filename="+new String(fileRealName.getBytes("gb2312"),"ISO8859-1" ));
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
        //读取目标文件，通过response将目标文件写到客户端  
        //获取目标文件的绝对路径 
		String fullFileName = UploadFileController.UPLOADFILEPATH+filename;
		//讀取文件
		try {
			InputStream is = new FileInputStream(fullFileName);
			OutputStream os = response.getOutputStream();
			//写文件
			int b = 0;
			while((b = is.read())!= -1){
				os.write(b);
			}
			is.close();
			os.close();
			result.put("message", true);
			result.put("info", "下载成功!");
		} catch (FileNotFoundException e) {
			log.info("下载文件沒有找到...");
			result.put("message", false);
			result.put("info", "下载成功!");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	*/
	
	/**
	 * isConnect:(判断网站url是否失效). <br/>
	 *
	 * @author liyang
	 * @param urlStr
	 * @return Date:2016年4月22日下午3:22:05
	 * @since JDK 1.6
	 */
	public boolean isConnect(String urlStr) {
		int counts = 0;
		URL url;
		HttpURLConnection con;
		int state = -1;
		if (urlStr == null || urlStr.length() <= 0) {
			return false;
		}
		while (counts < 5) {
			try {
				url = new URL(urlStr);
				con = (HttpURLConnection) url.openConnection();
				state = con.getResponseCode();
				System.out.println(counts + "= " + state);
				if (state == 200) {
					return true;
				}
				break;
			} catch (Exception ex) {
				counts++;
				System.out.println("URL不可用，连接第 " + counts + " 次");
				urlStr = null;
				continue;
			}
		}
		return false;
	}
	
	
}

