阅读背景:

从缓存的源文件中读取资源文件,下载到本地(包括图片和样式脚本)

来源:互联网 
/**   
 * @Title TempClass.java 
 * @Package com.metasoft.finance.controller 
 * @Description TODO(用一句话描述该文件做什么) 
 * @author zqs
 * @date 2014年4月22日 下午1:51:18 
 * @version V1.0   
*/
package com.metasoft.finance.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;

import com.metasoft.framework.pub.util.Constant;
import com.metasoft.framework.pub.util.Path;
import com.metasoft.framework.pub.util.StringUtil;

/** 
 * @ClassName TempClass 
 * @Description TODO(这里用一句话描述这个类的作用) 
 * @author zqs
 * @date 2014年4月22日 下午1:51:18 
 *  
 */
public class TempClass {

	public static void main(String[] args) {
		//读取指定的源文件,
		//从源文件中找到js,css,gif,ico 等文件的相对位置,
		//在本地创建相应的目录
		//从网上下载文件到相应位置
		String htmlPath="D:\developeProject\com";
		Map<String, String> resFileMap=getAllSourceFile(htmlPath);
		downloadFile("http","www.baidu.com",88,resFileMap,htmlPath);
	}
	private static Map<String, String> getAllSourceFile(String sourcePath){
//		List<String> sourceFileList=new ArrayList<String>();
		Map<String, String> resFileMap=new HashMap<String, String>();
		if(Path.isExistFolder(sourcePath)){
			File[] files=new File(sourcePath).listFiles();
			for (int i = 0; i < files.length; i++) {
				if(files[i].getName().endsWith(".do")){
					getAllDownloadFile(files[i].getAbsolutePath(),resFileMap);
				}
			}
		}
		return resFileMap;
	}
	private static Map<String, String> getAllDownloadFile(String sourceFile,Map<String, String> map){
		if(StringUtil.isEmpty(sourceFile)){
			return map;
		}
		List<String> sourceList=readFile(sourceFile);
		for (int i = 0; i < sourceList.size(); i++) {
			String tempStr=sourceList.get(i);
			String backStr="",resFile="";
			if(StringUtil.isNotEmpty(tempStr)&&tempStr.contains("src=\"/")){
				backStr=tempStr.substring(tempStr.indexOf("src=\"/")+"src=\"".length());
				resFile=backStr.substring(0, backStr.indexOf("\""));
			}else if(StringUtil.isNotEmpty(tempStr)&&tempStr.contains("href=\"/")){
				backStr=tempStr.substring(tempStr.indexOf("href=\"/")+"href=\"".length());
				resFile=backStr.substring(0, backStr.indexOf("\""));
			}
			if(StringUtil.isNotEmpty(resFile)&&!resFile.contains(".do")){
				map.put(resFile, resFile);
			}
		}
		
		return map;
	}
	private static void downloadFile(String protocol,String host,int port,Map<String, String> resFileMap,String targetPath){
		//先判断文件存不存在
		//不存在就判断目录存不存在,不存在就创建
		//存在目录就下载文件到本地相应的目录
		if(resFileMap==null||resFileMap.size()<=0){
			return;
		}
		Iterator it=resFileMap.keySet().iterator();
		while(it.hasNext()){
			String filePath=(String)it.next();
			File resFile=new File(targetPath+filePath);
			if(resFile.exists()){
				continue;
			}else{
				String savePath=resFile.getParentFile().getAbsolutePath();
				String fileName=resFile.getName();
				if(Path.isExistFolder(savePath)){
					String soureFullPath=protocol+"://"+host+":"+port+"/"+filePath;
					try {
						download(protocol,host,port,filePath, fileName, savePath);
					} catch (IOException e) {
						e.printStackTrace();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}			
		}
	}
	
	private static List<String> readFile(String strPathFile) {
		List<String> list=null;
		try {
			InputStreamReader isr=new InputStreamReader(new FileInputStream(strPathFile),Constant.ENCODED_UTF);
			BufferedReader br = new BufferedReader(isr);
			list=IOUtils.readLines(br);
			isr.close();
			br.close();
			isr=null;
			br=null;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
        return list;
	}
	public static void download(String protocol,String host,int port,String file, String filename,String savePath) throws Exception {  
        // 构造URL  
        URL url = new URL(protocol, host, port, file);
        // 打开连接  
        URLConnection con = url.openConnection();  
        //设置请求超时为5s  
        con.setConnectTimeout(5*1000);  
        // 输入流  
        InputStream is = con.getInputStream();  
      
        // 1K的数据缓冲  
        byte[] bs = new byte[1024];  
        // 读取到的数据长度  
        int len;  
        // 输出的文件流  
       File sf=new File(savePath);  
       if(!sf.exists()){  
           sf.mkdirs();  
       }  
       OutputStream os = new FileOutputStream(sf.getPath()+"\"+filename);  
        // 开始读取  
        while ((len = is.read(bs)) != -1) {  
          os.write(bs, 0, len);  
        }  
        // 完毕,关闭所有链接  
        os.close();  
        is.close();  
    }   
}
/**   
 * @Title TempClass.java 
 * @Package co



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: