Package test

Source Code of test.XxhhPic

/**
* (C) Copyright 2012 ZWW
*/
package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.UUID;

import jsoup.ConnectionManager;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import common.Config;

import cputils.XmlUtils;

/**
* @author ZWW
* @date 2014-3-22 下午5:09:37
* @version V1.0
*/
public class XxhhPic {
 
  private static String PIC_PATH = "E:\\xxhh-pic\\";

  /**
   * @param args
   */
  public static void main(String[] args) {
    //System.out.println(Integer.parseInt(s));
   
    //System.out.println(UUID.randomUUID().toString().replace("-", ""));
    //http://www.xxhh.com/page/4
    //getLogoFromWeb("", "temp/");
    XxhhPic pic = new XxhhPic();
    while(true){
      System.out.print("Input keyword:");
      Scanner sca = new Scanner(System.in)
      String queryStr = sca.next();
      if ("0".equals(queryStr)) {
        System.exit(0);
        sca.close();
      }else if("del".equals(queryStr)) {
        pic.deletePic(PIC_PATH);
      }else if("down".equals(queryStr)){
        pic.downPic("http://www.xxhh.com/page/", PIC_PATH);
      }
    }

//    pic.getAllPic();
   
  }
 
  public boolean deletePic(String filePath){
    File file = new File(filePath);
    if (file.isFile()) {
      deleteFile(file);
    }else {
      deleteDirectory(file.getAbsolutePath());
    }
   
//    for (File f : file.listFiles()) {
//      f.delete();
//    }
    return true;
  }
    /**
     * 删除单个文件
     * @param   sPath    被删除文件的文件名
     * @return 单个文件删除成功返回true,否则返回false
     */
    public boolean deleteFile(File file) {
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            System.out.println("delete file:"+file.getAbsolutePath());
        }
        return true;
    }
    /**
     * 删除目录(文件夹)以及目录下的文件
     * @param   sPath 被删除目录的文件路径
     * @return  目录删除成功返回true,否则返回false
     */
    public boolean deleteDirectory(String sPath) {
        //如果sPath不以文件分隔符结尾,自动添加文件分隔符
        if (!sPath.endsWith(File.separator)) {
            sPath = sPath + File.separator;
        }
        File dirFile = new File(sPath);
        //如果dir对应的文件不存在,或者不是一个目录,则退出
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return false;
        }
        //删除文件夹下的所有文件(包括子目录)
        File[] files = dirFile.listFiles();
        for (int i = 0; i < files.length; i++) {
            //删除子文件
            if (files[i].isFile()) {
              deleteFile(files[i]);
            } //删除子目录
            else {
                deleteDirectory(files[i].getAbsolutePath());
            }
        }
        //删除当前目录
        if (dirFile.delete()) {
          System.out.println("delete dir:"+dirFile.getAbsolutePath());
            return true;
        } else {
            return false;
        }
    }
 
  public void downPic(String url,String filePath) {
    int pageNo = 1;
    try {
      Document doc = ConnectionManager.getInstance().getDocument(url+pageNo);
      do {
        System.out.println("**********"+doc.baseUri()+"**********");
//        Elements elements = doc.select("div[class=container] > div[class=content]");
//        for (Element element : elements) {
//          System.out.println(element.html());
//        }
        Elements elements = doc.select("div[class^=bdshare_t]");
        JSONObject obj = null;
        for (Element element : elements) {
          String data = element.attr("data");
          try {
            if (data.indexOf("'title'") == -1) {
              continue;
            }
            data = data.replaceAll("\r\n", "");
            String title = data.substring(data.indexOf("'title'")+9,data.indexOf("'comment'")-2).trim();
            title = title.substring(0, title.length()-2);
            String pic = data.substring(data.indexOf("'pic'")+7,data.indexOf("'desc'")-2).trim();
            pic = pic.substring(0, pic.length()-2);
            data = "{"+data.substring(data.indexOf("'title'"));
//            System.out.println(data);
//            obj = JSONObject.fromObject(data);
//            if (obj.getString("pic").endsWith("jpg")||obj.getString("pic").endsWith("gif")) {
//              getLogoFromWeb(obj.getString("pic"), filePath+"\\"+pageNo+"\\",obj.getString("title"));
//            }
            if (pic.endsWith("jpg")||pic.endsWith("gif")) {
              getLogoFromWeb(pic, filePath,title);
//              getLogoFromWeb(pic, filePath+"\\"+pageNo+"\\",title);
            }
          } catch (Exception e) {
            System.out.println(data);
            e.printStackTrace();
          }
//          System.out.println(obj);
        }
//        Elements els = doc.getElementsByAttribute("_src");
//        //System.out.println(els.size());
//        if (els.size()>0) {
//          String imgUrl = "";
//          for (Element element : els) {
//            imgUrl = element.attr("tsrc");
//            if (StringUtils.isBlank(imgUrl)) {
//              imgUrl = element.attr("_src");
//            }
//            //System.out.println(imgUrl);
//            getLogoFromWeb(imgUrl, filePath);
//          }
//        }
        doc = ConnectionManager.getInstance().getDocument(url+(++pageNo));
      } while (doc.text().contains("下一页"));
      System.out.println("**********END**********");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
  public File getLogoFromWeb(String logourl, String uploadPath,String fileName) {
        try {
            String fileType = logourl.substring(logourl.lastIndexOf("."), logourl.length());
            File dirFile = new File(uploadPath);
            if (!dirFile.exists()) {
        dirFile.mkdir();
      }
            File outFile = new File(uploadPath + fileName + fileType);
            System.out.println(fileName + fileType);
            if (outFile.exists()) {
        return outFile;
      }
            if (!isExisted(fileName + fileType)) {
              putFileName(fileName + fileType);
            }
            outFile.createNewFile();
            OutputStream os = new FileOutputStream(outFile);
            URL url = new URL(logourl);
            InputStream is = url.openStream();
            byte[] buff = new byte[1024];
            while (true) {
                int readed = is.read(buff);
                if (readed == -1) {
                    break;
                }
                byte[] temp = new byte[readed];
                System.arraycopy(buff, 0, temp, 0, readed);
                os.write(temp);
            }
            is.close();
            os.close();
            return outFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
  public File getLogoFromWeb(String logourl, String uploadPath) {
        try {
            return getLogoFromWeb(logourl, uploadPath, UUID.randomUUID().toString().replace("-", ""));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
  public static boolean isExisted(String content){
    boolean had = false;
    try {
      FileReader fileReader = new FileReader(new File(Config.DATA_FILE_PATH+"/xxhhPicName.txt"));
      BufferedReader reader = new BufferedReader(fileReader);
      String con;
      while ((con = reader.readLine()) != null) {
        if (content.equals(con)) {
//          System.out.println(fileName+" is existed..");
          had = true;
          break;
        }
      }
      reader.close();
      fileReader.close();
    } catch (FileNotFoundException e) {
      System.err.println(e.getMessage());
    } catch (IOException e) {
      System.err.println(e.getMessage());
    }
    return had;
  }
 
  private List<String> getAllPic(){
    System.out.println("****put fileName begin...");
    List<String> list = new ArrayList<String>();
    File dirfile = new File(PIC_PATH);
    if (!dirfile.exists()) {
      System.err.println("dir not found...");
      return list;
    }
    for (File f : dirfile.listFiles()) {
      for (String s : f.list()) {
        list.add(s);
        if (!isExisted(s)) {
          putFileName(s);
        }
      }
    }
    System.out.println("****put fileName ok...");
    return list;
  }
 
  private void putFileName(String fileName){
    XmlUtils.writeDataFile(fileName+"\r\n", "xxhhPicName.txt", true);
//    System.out.println("****put fileName: "+fileName);
  }

}
TOP

Related Classes of test.XxhhPic

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.