首页>代码>Httpclient的使用实例>/HttpClient使用/Tool.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ssszm;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import java.util.regex.*;
/**
 *
 * @author seelespirit
 */
import net.htmlparser.jericho.Source;
import org.apache.commons.httpclient.methods.PostMethod;
import org.json.JSONObject;

public class Tool {

    /**
     * 获得图片列表
     * @return 图片地址列表
     */
    public String[] getPictureUrls(String url) {
        Http http = Http.getInstance(url);
        HttpClient httpClient = http.getClient();
        GetMethod getMethod = new GetMethod(url);
        getMethod.setRequestHeader("User-Agent", "MSIE 6.0");

        try {
            httpClient.executeMethod(getMethod);
            String html = getMethod.getResponseBodyAsString();
            html = html.replaceAll("\r", "");
            return html.split("\n");
        } catch (Exception e) {
            this.logError(e);
            return null;
        }
    }

    /**
     * 保存图片到本地地址
     * @param picUrl 图片地址
     * @param filePath 保存文件夹,从配置文件中取得,后跟斜线,例 c:/ d:/tmp/
     * @return 保存图片
     */
    public boolean savePicture(String picUrl, String filePath, String processPath, int errCounter) {
        try {
            String strTmp = picUrl;
            String fileName = this.config("DEFAULT_PIC_HD_PATH_PREFIX");
            //处理当前图片地址,按照|分割,第一段为目标url,第二段为本地路径
            picUrl = strTmp.substring(0, (strTmp.indexOf("|")));
            fileName += strTmp.substring((strTmp.indexOf("|")+1), strTmp.length());
            //验证当前路径是否可用,不可用则生成路径
            File filePathTmp = new File(filePath);
            if (!filePathTmp.exists()) {
                filePathTmp.mkdirs();
            }
            //获取图片内容
            GetMethod getMethod = this.curl(picUrl);
            //生成本地图片地址
            //fileName = filePath + fileName;
            //得到网络资源的字节数组,并写入文件
            FileOutputStream fos = new FileOutputStream(new File(fileName));
            fos.write(getMethod.getResponseBody());
            fos.close();

            //写进度文件
            Main.process_current++;
            String processFile = processPath + Main.process_main_id + ".txt";
            File pFile = new File(processFile);
            if(!pFile.exists()){
                pFile.createNewFile();
            }
            fos = new FileOutputStream(pFile);
            int process = Main.process_current;
            if(Main.process_total == (Main.process_current+1)){
                process = 100;
            }else{
                double pTmp = (double)Main.process_current / Main.process_total;// 打印计算结果
                pTmp = pTmp * 100;
                String sTmp = String.valueOf(pTmp);
                sTmp = sTmp.substring(0, sTmp.indexOf("."));
                process = Integer.parseInt(sTmp);
            }
            fos.write(String.valueOf(process).getBytes());
            fos.close();
            //this.savePictureEnd(this.config("SAVE_PIC_END_CALLBACK"));
            return true;
        } catch (Exception e) {
            this.logError(e);
            if(errCounter >= Integer.parseInt(this.config("MAX_RETRY_TIMES"))){
                ArrayList al = new ArrayList();
                al.add(picUrl);
                //this.err(4, al);
            }else{
                ArrayList al = new ArrayList();
                al.add(picUrl);
                //this.err(5, al);
                savePicture(picUrl, filePath, processPath, ++errCounter);
            }
            return false;
        }
    }

    /**
     * 保存成功以后调用此函数,请求服务器
     * @param url
     * @return
     */
    public boolean savePictureEnd(String url){
        GetMethod getMethod = this.curl(url);
        if(getMethod.getStatusCode() == 200 || getMethod.getStatusCode() == 304){
            //TODO: 这里放保存图片完毕后回调操作代码
            return true;
        }
        return false;
    }

    /**
     * 创建请求模块
     * @param url
     * @return GetMethod
     */
    public GetMethod curl(String url) {
        Http http = Http.getInstance(url);
        HttpClient httpClient = http.getClient();
        httpClient.getParams().setParameter("http.socket.timeout", 1); //   为HttpClient设置参数
        httpClient.getHttpConnectionManager().getParams().setParameter("http.socket.timeout", 10); //   为HttpConnetionManager设置参数
        GetMethod getMethod = new GetMethod(url);
        getMethod.getParams().setParameter("http.socket.timeout", 10000); //   为HttpMethod设置参数
        try {
            httpClient.executeMethod(getMethod);
            return getMethod;
        } catch (IOException ioe) {
            this.logError(ioe);
            return null;
        }
    }
    public String getHttpContent(String url) throws IOException {
        Http http = Http.getInstance(url);
        HttpClient httpClient = http.getClient();
        httpClient.getParams().setParameter("http.socket.timeout", 1); //   为HttpClient设置参数
        httpClient.getHttpConnectionManager().getParams().setParameter("http.socket.timeout", 10); //   为HttpConnetionManager设置参数
        GetMethod getMethod = new GetMethod(url);
        getMethod.getParams().setParameter("http.socket.timeout", 10000); //   为HttpMethod设置参数
        httpClient.executeMethod(getMethod);
        InputStream inputStream = getMethod.getResponseBodyAsStream();
//        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
//                inputStream, "UTF-8"));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                inputStream,getMethod.getResponseCharSet()));
        String line;
        StringBuffer accessline = new StringBuffer();
        while ((line = bufferedReader.readLine()) != null) {
            accessline.append(line);
        }
        return accessline.toString();
    }
    public String getFileContent(String filePath) {
        File file = new File(filePath);
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            BufferedReader bufferedReader =  new BufferedReader(new InputStreamReader(fileInputStream));
            String content = "";
            String strTmp = "";
            int i = 0;
            while((strTmp=bufferedReader.readLine())!=null){
                if(i>0) content += "\r\n";
                content += strTmp;
                i++;
            }
            return content;
        } catch (IOException ioe) {
            this.logError(ioe);
        }
        return null;
    }

    public String postTo(String url, JSONObject j) {
        Http http = Http.getInstance(url);
        HttpClient httpClient = http.getClient();
        httpClient.getParams().setParameter("http.socket.timeout", 10000); //   为HttpClient设置参数
        httpClient.getHttpConnectionManager().getParams().setParameter("http.socket.timeout", 100);
        PostMethod postMethod = new PostMethod(url);
        NameValuePair[] nameValuePair = {new NameValuePair("data", j.toString())};
        postMethod.setRequestBody(nameValuePair);
        try{
            httpClient.executeMethod(postMethod);
            return postMethod.getResponseBodyAsString();
        }catch(Exception e){
            this.logError(e);
            return "ERROR : 执行失败,请检查API接口是否有误:" + url;
        }
    }
    /**
     * 将内容写入文件
     * @param sth 写入内容
     * @param file 写入文件路径
     * @param add1new0 true添加 false清空后写入
     * @return 
     */
    public boolean writeSthToFile(String sth, String file, boolean add1new0){
        System.out.println(sth);
        FileOutputStream fos;
        File pFile = new File(file);
        if(!pFile.exists()){
            try{
                pFile.createNewFile();
            }catch(IOException ioe){
                this.logError(ioe);
            }
        }
        try{
            fos = new FileOutputStream(pFile, add1new0);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String logDate = df.format(new Date());
            sth = logDate + " " + sth + "\r\n";
            fos.write(String.valueOf(sth).getBytes());
            fos.close();
            return true;
        }catch(FileNotFoundException fnfe){
            this.logError(fnfe);
        }catch(IOException ioe){
            this.logError(ioe);
        }
        return false;
    }

    /**
     * 记录错误日志
     * @param ex 异常
     * @return
     */
    public boolean logError(Exception ex){
        StackTraceElement ste= ex.getStackTrace()[0];
        String sth = ste.getClassName();
        sth += " Throws Exception : " + ex.toString();
        sth += " In : " + ste.getFileName();
        sth += " ("+ste.getLineNumber()+")";
        FileOutputStream fos;
        File pFile = new File(new File(".").getAbsolutePath() + "/error.log");
        if(!pFile.exists()){
            try{
                pFile.createNewFile();
            }catch(IOException ioe){
                this.logError(ioe);
            }
        }
        try{
            fos = new FileOutputStream(pFile, true);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String logDate = df.format(new Date());
            sth = logDate + " " + sth + "\r\n";
            fos.write(String.valueOf(sth).getBytes());
            fos.close();
            return true;
        }catch(FileNotFoundException fnfe){
            this.logError(fnfe);
        }catch(IOException ioe){
            this.logError(ioe);
        }
        return false;
    }
    
    //读取配置文件
    public String config(String key) {
        try {
            Properties props = new Properties();
            File directory = new File(".");
            String logFilePath = "";
            String confFilePath = directory.getAbsolutePath() + "/conf.properties";
            InputStream in=new BufferedInputStream(new FileInputStream(confFilePath));
            //props.load(getClass().getClassLoader().getResourceAsStream("conf.properties"));
            props.load(in);
            String strTmp = "";
            try {
                strTmp = props.getProperty(key).toString();
                strTmp = new String(props.getProperty(key).getBytes("ISO-8859-1"), "GBK");
            } catch (Exception e) {
                System.out.println("Try to find file " + confFilePath);
                this.logError(e);
                //e.printStackTrace();
                return null;
            }
            return strTmp;
        } catch (IOException ioe) {
            this.logError(ioe);
            return null;
        }
    }
    public String config(String key, String[] vals) {
        try {
            Properties props = new Properties();
            File directory = new File(".");
            String logFilePath = "";
            String confFilePath = directory.getAbsolutePath() + "/conf.properties";
            InputStream in=new BufferedInputStream(new FileInputStream(confFilePath));
            //props.load(getClass().getClassLoader().getResourceAsStream("conf.properties"));
            props.load(in);
            String strTmp = "";
            try {
                strTmp = props.getProperty(key).toString();
                for(int i=1; i<=vals.length; i++){
                    strTmp = strTmp.replaceAll("%s" + i, vals[i-1]);
                }
                strTmp = new String(strTmp.getBytes("ISO-8859-1"), "GBK");
            } catch (Exception e) {
                System.out.println("Try to find file " + confFilePath);
                this.logError(e);
                //e.printStackTrace();
                return null;
            }
            return strTmp;
        } catch (IOException ioe) {
            this.logError(ioe);
            return null;
        }
    }
    //读取配置文件init
    public String init(String key) {
        try {
            Properties props = new Properties();
            File directory = new File(".");
            String logFilePath = "";
            String confFilePath = directory.getAbsolutePath() + "/init.properties";
            InputStream in=new BufferedInputStream(new FileInputStream(confFilePath));
            props.load(in);
            String strTmp = "";
            try {
                strTmp = props.getProperty(key).toString();
                strTmp = new String(props.getProperty(key).getBytes("ISO-8859-1"), "GBK");
            } catch (Exception e) {
                System.out.println("Try to find file " + confFilePath);
                this.logError(e);
                return null;
            }
            return strTmp;
        } catch (IOException ioe) {
            this.logError(ioe);
            return null;
        }
    }
    
    
//    public static void main(String[] args){
//        Tool tool = new Tool();
//        JSONObject j = new JSONObject();
//        try{
//            j.put("a", "b");
//            j.put("c", "d");
//            j.put("e", "f");
//            j.put("g", "h");
//        }catch(Exception e){
//
//        }
//        tool.postTo("http://localhost/t.php", j);
//
//    }
}
最近下载更多
1358849392  LV21 2023年10月12日
xianyu091012  LV4 2023年7月14日
fdsfsffds  LV1 2022年8月4日
糙米caomi  LV1 2021年4月9日
阿昌先生  LV13 2021年1月18日
lironggang  LV38 2020年11月26日
carrot33  LV1 2020年7月30日
Call Me 采先森  LV10 2020年5月15日
xtm123  LV18 2020年3月11日
sleepcat88  LV13 2020年1月20日
最近浏览更多
雨中纸鹤  LV1 2023年12月6日
xianyu091012  LV4 2023年7月14日
fdsfsffds  LV1 2022年8月4日
mq13947193109  LV19 2022年7月5日
wangdengzhe  LV7 2022年4月6日
ssh123  LV10 2021年11月18日
我是helloworld  LV23 2021年11月17日
newhaijun  LV15 2021年7月12日
gao123qq  LV21 2021年5月7日
糙米caomi  LV1 2021年4月9日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友