⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bottool.java

📁 用java实现的一个bbs的portal
💻 JAVA
字号:
package BBSSpider;

import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;

//to support TextSpider And TextParser With some useful functions


public class BotTool {

  public static String doPost(String theurl,String paramString) throws IOException{
    URL url=new URL(theurl);
    HttpURLConnection connection=(HttpURLConnection)(url.openConnection());
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    PrintWriter out= new PrintWriter(connection.getOutputStream());

    out.print(paramString);

    out.close();

    BufferedReader in;
    try{
      in= new BufferedReader(new InputStreamReader(connection.getInputStream()));
    }
    catch(FileNotFoundException e){


      InputStream err=connection.getErrorStream();
      if (err==null)throw e;
      in =new BufferedReader(new InputStreamReader(err));

    }
    StringBuffer response=new StringBuffer();
    String line;
    while((line=in.readLine())!=null)
      response.append(line+"\n");
    in.close();
    //saveToFile(response.toString(),"lala.html");

    return response.toString();
  }

  public static String doGet(String theurl) throws IOException{
	 // System.err.println("enter doGet:"+theurl);
    URL url=null;
    HttpURLConnection connection=null;
    try{
        url=new URL(theurl);
        connection=(HttpURLConnection)(url.openConnection());
    }
    catch(Exception e){
		    //System.err.println("open url connection failed");
        e.printStackTrace();
    }

	//System.err.println("got connection:"+theurl);
    //先获取ResponseCode
    int responseCode=connection.getResponseCode();
    if (!((responseCode==200)||(responseCode==401))){
      System.out.println("HTTP Response:"+connection.getResponseMessage());
      return null;
    }

    //System.err.println("begin read stream:"+theurl);
    BufferedReader in;
    try{
      in= new BufferedReader(new InputStreamReader(connection.getInputStream()));
    }
    catch(FileNotFoundException e){
      InputStream err=connection.getErrorStream();
      if (err==null) throw e;
      in =new BufferedReader(new InputStreamReader(err));
      throw e;
    }
    StringBuffer response=new StringBuffer();
    String line;
    while((line=in.readLine())!=null){
		  //  System.err.println(line);
        response.append(line+"\n");
		}
    in.close();

	//System.err.println("read stream over:"+theurl);
    return response.toString();
  }

  public static void saveToFile(String page,String FileName){
    FileWriter fileOS=null;
    PrintWriter pWriter=null;
		System.err.println("filename:"+FileName);
    try{
      fileOS=new FileWriter(FileName);
    }
    catch (IOException e){
      System.out.println("file not find");
    }

    try {
      pWriter=new PrintWriter(new BufferedWriter(fileOS));
      pWriter.print(page);
    }
    catch (Exception e) {
      System.out.println("Error in I/O:" + e.getMessage());
			e.printStackTrace();
    }

    try{
      pWriter.close();
    }
    catch(Exception e){
    }
    //System.out.println("搞定了");
  }

  public static void saveToFile(String page,String FileName,boolean append){
    FileWriter fileOS=null;
    PrintWriter pWriter=null;
		System.err.println("filename:"+FileName);
    try{
      fileOS=new FileWriter(FileName,append);
    }
    catch (IOException e){
      System.out.println("file not find");
    }

    try {
      pWriter=new PrintWriter(new BufferedWriter(fileOS));
      pWriter.print(page);
    }
    catch (Exception e) {
      System.out.println("Error in I/O:" + e.getMessage());
			e.printStackTrace();
    }

    try{
      pWriter.close();
    }
    catch(Exception e){
    }
    //System.out.println("搞定了");
  }

  public static String loadFromFile(String FileName){
    FileReader fileRead=null;
    BufferedReader bufReader=null;
    try{
      fileRead=new FileReader(FileName);
    }
    catch (IOException e){
      System.out.println("file not find");
    }

    try {
      bufReader=new BufferedReader(new BufferedReader(fileRead));
    }
    catch (Exception e) {
      System.out.println("Error in I/O:" + e.getMessage());
    }

    StringBuffer response=new StringBuffer();
    String line;
    try{
    while((line=bufReader.readLine())!=null)
      response.append(line+"\n");
    }
    catch(Exception e){
    }
    try{
      bufReader.close();
    }
    catch(Exception e){

    }

    return response.toString();
  }

  public static String replaceString(String srcStr,String oldStr,String newStr){
      String foreStr=srcStr.substring(0,srcStr.indexOf(oldStr));
      String postStr=srcStr.substring(srcStr.indexOf(oldStr)+oldStr.length());
      return foreStr+newStr+postStr;
  }

  public static boolean makeDir(String FileName){
    //EntryURL.substring(EntryURL.length()-4,EntryURL.length())
      File HTMLdir=new File(FileName);
    try{
      HTMLdir.mkdirs();
      if (HTMLdir.exists()){
        System.out.println("目录已经生成");
        return true;
      }
      else {
        System.out.println("目录不存在");
        return false;
      }

    }
    catch(Exception e){
      System.out.println("不能创建新的目录");
      return false;
    }

  }

  public static String getChineseStr(String temp)
    {
        String dbstr="";
        try{
            if (temp!=null)
            {
                byte[] dbbyte=temp.getBytes("iso-8859-1");
                dbstr=new String(dbbyte);
            }else {
                dbstr="空";
            }
        }
        catch(Exception e){
            return temp;
        }
        return dbstr;
    }

    public static String Date2String(Date date,String regStr){
         if (date==null){
             date=new java.util.Date();
         }
         SimpleDateFormat myFormat = new SimpleDateFormat(regStr);
         return  myFormat.format(date);
    }

    public static String MMM2MM(String dateStr){
            if (dateStr.indexOf("Jan")!=-1)
            {
                return dateStr.replaceFirst("Jan","01");
            }
            else
                if (dateStr.indexOf("Feb")!=-1)
                {
                    return dateStr.replaceFirst("Feb","02");
                }
                else
                    if (dateStr.indexOf("Mar")!=-1)
                    {
                        return dateStr.replaceFirst("Mar","03");
                    }
                    else
                        if (dateStr.indexOf("Apr")!=-1)
                        {
                            return dateStr.replaceFirst("Apr","04");
                        }
                        else
                            if (dateStr.indexOf("May")!=-1)
                            {
                                return dateStr.replaceFirst("May","05");
                            }
                            else
                                if (dateStr.indexOf("Jun")!=-1)
                                {
                                    return dateStr.replaceFirst("Jun","06");
                                }
                                else
                                    if (dateStr.indexOf("Jul")!=-1)
                                    {
                                        return dateStr.replaceFirst("Jul","07");
                                    }
                                    else
                                        if (dateStr.indexOf("Aug")!=-1)
                                        {
                                            return dateStr.replaceFirst("Aug","08");
                                        }
                                        else
                                            if (dateStr.indexOf("Sep")!=-1)
                                            {
                                                return dateStr.replaceFirst("Sep","09");
                                            }
                                            else
                                                if (dateStr.indexOf("Oct")!=-1)
                                                {
                                                    return dateStr.replaceFirst("Oct","10");
                                                }
                                                else
                                                    if (dateStr.indexOf("Nov")!=-1)
                                                    {
                                                        return dateStr.replaceFirst("Nov","11");
                                                    }
                                                    else
                                                        if (dateStr.indexOf("Dec")!=-1)
                                                        {
                                                            return dateStr.replaceFirst("Dec","12");
                                                        }
            return dateStr;
        }
        public static Date String2Date(String dateStr,String regStr){
            SimpleDateFormat myFormat = new SimpleDateFormat(regStr);
            try {
                return  myFormat.parse(BotTool.MMM2MM(dateStr));
            } catch (ParseException e) {
                //e.printStackTrace();  //To change body of catch statement use Options | File Templates.
                System.out.println("can't convert the date String:"+dateStr+" to Date");
                return null;
            }
        }


  public BotTool() {
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -