package com.trs.web2frame.tool.tyjyh;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.trs.web2frame.tool.tyjyh.ConfigConstant;
import com.trs.web2frame.tool.tyjyh.tonglaozheng.ColumnConfig;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class FujianUtil{
	//上传方式
	public static String getFullPathUpload(String filePath) throws Exception {
		try {
			String first = filePath.substring(0, 8);
			String second = filePath.substring(0, 10);
			String type = filePath.substring(0, 2);
			String newUrl= "";
			if ("W0".equals(type)) {
				newUrl="src=\"/upload/"+first+"/"+second+"/"+filePath+"\""+" uploadpic=\""+filePath+"\"";
			} else {
				newUrl="href=\"/upload/"+first+"/"+second+"/"+filePath+"\""+" appendix='true'";
			}
			return newUrl;
		} catch (Exception e) {
			return "";
		}
    }
	

//    public static String root1;
//    public static String root2;
      // 特定网站的附件特定处理
	/**
     * 处理内容中附件路径
     * @return
     * @throws Exception
     */
	public static String getFullPath(String filePath) throws Exception {
		String root2="http://192.156.41.118:8081/trs/";
		try {
			String type = filePath.substring(0, 2);
		if ("W0".equals(type)) {
			root2 += "webpic1/";
		} else {
			root2 += "protect1/";
		}
		String first = filePath.substring(0, 8);
		String second = filePath.substring(0, 10);
		String result = root2 + first + "/" + second + "/" + filePath;
		return result;
		} catch (Exception e) {
			return "";
		}
    }

    // 文件(包含视频)
	public static String getLocalFullPath(String file) {
		String root2=ConfigConstant.REMOTEURL;
		try {
			String type = file.substring(0, 2);
		if ("W0".equals(type)) {
			root2 += "webpic/";
		} else if("P0".equals(type)){
			root2 += "protect/";
		}else {
			return "";
		}
		String first = file.substring(0, 8);
		String second = file.substring(0, 10);
		String result = root2 + first + "/" + second + "/" + file;
		return result;
		} catch (Exception e) {
			return "";
		}
	}
	
	
	public static String getLocalFullPathDownloadFiles(String file) {
		String root2=ConfigConstant.HTTPLOADURL;
		try {
			String type = file.substring(0, 2);
		if ("W0".equals(type)) {
			root2 += "webpic/";
		} else {
			root2 += "protect/";
		}
		String first = file.substring(0, 8);
		String second = file.substring(0, 10);
		String result = root2 + first + "/" + second + "/" + file;
		
		downLoadFromUrl(result,
				file,ConfigConstant.TEMPLOCALFILE );
		return ConfigConstant.TEMPLOCALFILE+"\\"+file;
		} catch (Exception e) {
			return "";
		}
	}
	
	public static String getLocalFullPathDownload(String file) {
//		String root2="http://lcj.shanxi.gov.cn/";
//		String root2="http://fgw.shanxi.gov.cn/";
//		String root2="http://nynct.shanxi.gov.cn/";
//		String root2="http://www.shanxi.gov.cn/";
//		String root2="http://sjt.shanxi.gov.cn/";
		String root2="http://zrzyt.shanxi.gov.cn/";
		String rootstr = "";
		try {
			String type = file.substring(0, 2);
		if ("W0".equals(type)) {
			root2 += "webpic/";
			rootstr = "webpic/";
		} else {
//			return "";
			root2 += "protect/";
			rootstr = "protect/";
		}
		String first = file.substring(0, 8);
		String second = file.substring(0, 10);
		String result = root2 + first + "/" + second + "/" + file;
		//String fileExits = "F:\\山西省各单位数据\\21-自然资源厅\\download\\zrzyt\\"+ rootstr + first + "/" + second+"/"+file;
		//File file22 = new File(fileExits);
		//if(file22.exists())return "";
		//downLoadFromUrl(result,
		//		file,"F:\\山西省各单位数据\\21-自然资源厅\\download3\\zrzyt\\"+ rootstr + first + "/" + second  );
//		downLoadFromUrl(result,
//				file,"G:\\download\\szf\\"+ rootstr + first + "/" + second  );
		
		//return "D:\\拓尔思文档\\参与的项目\\天津集约化\\temp\\"+file;
		} catch (Exception e) {
			e.printStackTrace();

		}
		return "";
	}
	
	
	public static String getLocalFullPathDownload2222(String file) {
        //String root = "";
		//String root = "";
		String root2="http://192.156.41.118:8081/trs";
		String result = root2  + file;
		//String filess = file.substring(file.lastIndexOf("/")+1, file.length());
		/*try {
			downLoadFromUrl(result,
					filess,"D:\\托尔思文档\\参与的项目\\天津集约化\\temp");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		return result;
	}
	
	public static void  downLoadFromUrl(String urlStr,String fileName,String savePath) throws IOException{
		URL url = new URL(urlStr);  
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
                //设置超时间为3秒
		conn.setConnectTimeout(300*1000);
		//防止屏蔽程序抓取而返回403错误
		conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
		//得到输入流
		InputStream inputStream = conn.getInputStream();  
//		//获取自己数组
//		byte[] getData = readInputStream(inputStream);    
 
		//文件保存位置
		File saveDir = new File(savePath);
		if(!saveDir.exists()){
			saveDir.mkdirs();
		}

		File file = new File(saveDir+File.separator+fileName);    
		FileOutputStream fos = new FileOutputStream(file);   
        byte[] buffer = new byte[1000];
        int len;

        while ((len = inputStream.read(buffer)) > 0) {
        	fos.write(buffer, 0, len);
        }
//		fos.write(getData);
		if(fos!=null){
			fos.close();  
		}
		if(inputStream!=null){
			inputStream.close();
		}
		System.out.println("info:"+url+" download success");
	}
 
	/**
	 * 从输入流中获取字节数组
	 * @param inputStream
	 * @return
	 * @throws IOException
	 */
	public static  byte[] readInputStream(InputStream inputStream) throws IOException {  
		byte[] buffer = new byte[4096];  
		int len = 0;  
		ByteArrayOutputStream bos = new ByteArrayOutputStream();  
		while((len = inputStream.read(buffer)) != -1) {  
			bos.write(buffer, 0, len);  
		}  
		bos.close();  
		return bos.toByteArray();  
	} 
	
	public static String getHttpFilePath(String file) {
		String root2=ConfigConstant.HTTPLOADURL;
		try {
			String type = file.substring(0, 2);
		if ("W0".equals(type)) {
			root2 += "webpic/";
		} else if ("P0".equals(type)) {
			root2 += "protect/";
		} else if ("U0".equals(type)) {
			root2 += "upload/";
		}
		String first = file.substring(0, 8);
		String second = file.substring(0, 10);
		String result = root2 + first + "/" + second + "/" + file;
		return result;
		
		} catch (Exception e) {
			return "";
		}
	}
	
	
	public static void main(String[] args) throws IOException {
		String url = "https://www.runoob.com/html/html5-video.html";
		Document doc = Jsoup.parse(new URL(url), 10000);
		String html = doc.toString();
		Elements linkvideo = doc.select("source[src]");
		for (Element element : linkvideo) {
			String sHtml = element.toString();
			String textsrc = element.attr("src");
			try {

				String iframeHtml = "<p>\n" +
						"<iframe frameborder=\"0\" src=\"/"+url+"\" width=\"400\" height=\"225\"></iframe>\n" +
						"</p><p>\n" +
						"<br/>\n" +
						"</p>";
				html = html.replace(sHtml,iframeHtml);
			}catch (Exception e){
				e.printStackTrace();
			}
		}
		System.out.println(html);
	}
	
}