Commons是apache下属的开源子项目,专门提供一些通用的工具类。
这些方法用起来后,程序看起来更加简单,专业。
Commons的主要类结构:
Attributes
|
Runtime API to metadata attributes such as doclet tags.
|
BeanUtils
|
Easy-to-use wrappers around the Java reflection and introspection APIs.
|
Betwixt
|
Services for mapping JavaBeans to XML documents, and vice versa.
|
Chain
|
Chain of Responsibility pattern implemention.
|
CLI
|
Command Line arguments parser.
|
Codec
|
General encoding/decoding algorithms (for example phonetic, base64, URL).
|
Collections
|
Extends or augments the Java Collections Framework.
|
Compress
|
Defines an API for working with tar, zip and bzip2 files.
|
Configuration
|
Reading of configuration/preferences files in various formats.
|
Daemon
|
Alternative invocation mechanism for unix-daemon-like java code.
|
DBCP
|
Database connection pooling services.
|
DbUtils
|
JDBC helper library.
|
Digester
|
XML-to-Java-object mapping utility.
|
Discovery
|
Tools for locating resources by mapping service/reference names to resource names.
|
EL
|
Interpreter for the Expression Language defined by the JSP 2.0 specification.
|
Email
|
Library for sending e-mail from Java.
|
Exec
|
API for dealing with external process execution and environment management in Java.
|
FileUpload
|
File upload capability for your servlets and web applications.
|
IO
|
Collection of I/O utilities.
|
JCI
|
Java Compiler Interface
|
Jelly
|
XML based scripting and processing engine.
|
Jexl
|
Expression language which extends the Expression Language of the JSTL.
|
JXPath
|
Utilities for manipulating Java Beans using the XPath syntax.
|
Lang
|
Provides extra functionality for classes in java.lang.
|
Launcher
|
Cross platform Java application launcher.
|
Logging
|
Wrapper around a variety of logging API implementations.
|
Math
|
Lightweight, self-contained mathematics and statistics components.
|
Modeler
|
Mechanisms to create Model MBeans compatible with JMX specification.
|
Net
|
Collection of network utilities and protocol implementations.
|
Pool
|
Generic object pooling component.
|
Primitives
|
Smaller, faster and easier to work with types supporting Java primitive types.
|
Proxy
|
Library for creating dynamic proxies.
|
Sanselan
|
A pure-Java image library.
|
SCXML
|
An implementation of the State Chart XML specification aimed at creating and maintaining a Java SCXML engine. It is capable of executing a state machine defined using a SCXML document, and abstracts out the environment interfaces.
|
Transaction
|
Implementations for multi level locks, transactional collections and transactional file access.
|
Validator
|
Framework to define validators and validation rules in an xml file.
|
VFS
|
Virtual File System component for treating files, FTP, SMB, ZIP and such like as a single logical file system.
|
Lang
Lang
Provides extra functionality for classes in java.lang.
org.apache.commons.lang.builder.
ToStringBuilder:
提供object重写toString()的工具类.
ApplicationToStringStyle.APP_TO_STRING_STYLE提供toString的style。
Fruit中重写了toString方法。
public abstract class ApplicationToStringStyle extends ToStringStyle {
private static final long serialVersionUID = 1L;
public static final ApplicationToStringStyle APP_TO_STRING_STYLE = new CustomToStringStyle();
/**
* Constructor
*
* Use the static constant rather than instantiating
*/
private static final class CustomToStringStyle extends ApplicationToStringStyle {
private static final long serialVersionUID = -4289672936337739486L;
public CustomToStringStyle() {
super();
this.setUseClassName(true);
this.setUseIdentityHashCode(false);
this.setContentStart(SystemUtils.LINE_SEPARATOR + "[");
this.setFieldSeparator(SystemUtils.LINE_SEPARATOR + " ");
this.setFieldSeparatorAtStart(true);
this.setContentEnd(SystemUtils.LINE_SEPARATOR + "]");
this.setUseShortClassName(true);
}
/**
* Override the append() method to skip fields that start with "_". In this
* application, Axis generates some fields using this prefix that we don't want to include
* in the log files.
*/
@Override
public void append(StringBuffer buffer, String fieldName, Object value, Boolean fullDetail) {
if (fieldName != null && !fieldName.startsWith("_")) {
super.append(buffer, fieldName, value, fullDetail);
}
}
/**
* Ensure Singleton after serialization
*
* @return the singleton
*/
private Object readResolve() {
return ApplicationToStringStyle.APP_TO_STRING_STYLE;
}
}
}
public class Fruit {
private String name = "apple";
private String size = "3";
private String price = "$40";
public String toString() {
return ReflectionToStringBuilder.toString(this, ApplicationToStringStyle.APP_TO_STRING_STYLE);
}
}
HttpClient
The Commons HttpClient project used to be a part of Commons, but is now part of
Apache HttpComponents
-
see Jakarta Commons
HttpClient
用于http有关的连接,包括ssl等。
Notice that we go
through the entire process regardless of whether the server returned an error or
not. This is important because HTTP 1.1 allows multiple requests
to use the same
connection by simply sending the requests one after the other.
Obviously, if we
don't read the entire response to the first request, the left over data will get
in the way of the second response. HttpClient
tries to handle this but
to avoid problems it is important to always release the connection.
基本http get访问
:
package commons.httpclient;
import java.io.IOException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class Test {
public static void main(String[] args) throws HttpException, IOException {
//1. Create an instance of HttpClient.
HttpClient client = new HttpClient();
//2.
//Create an instance of one of the methods (GetMethod in this case).
//The URL to connect to is passed in to the the method constructor.
GetMethod method = new GetMethod("http://www.baidu.com/");
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
//3.Tell HttpClient to execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
//4.Read the response.
String reponse = method.getResponseBodyAsString();
//5.Release the connection.
method.releaseConnection();
//6.Deal with the response.
System.out.println(reponse);
}
}
分享到:
相关推荐
Apache Commons ID 和 Commons Discovery 是两个在Java开发中广泛使用的开源库,它们是Apache软件基金会的项目,为开发者提供了丰富的工具和功能。 Apache Commons ID 主要关注于生成唯一标识(ID)的功能,它提供...
在实际开发中,引入"apache-commons-logging.jar"到项目中,可以通过Maven、Gradle等构建工具,或者直接将JAR文件添加到项目的类路径中。对于依赖管理,可以设置相关的依赖配置,例如在Maven的pom.xml文件中添加如下...
刚刚从apache官网下载的apache-commons的常用工具jar包,都是最新版本,有源码包,有api,包括了15种常用工具jar,比如common-lang,commons-collections,commons-io等等。也简单说明了每种工具包的用途。
9. **Commons DbUtils**: 是一个轻量级的数据库工具包,简化了 JDBC 的使用,如自动关闭资源、执行 SQL 语句等。它的 `QueryRunner` 类提供了执行 SQL 查询和更新操作的简便方法。 这些 API 在实际开发中有着广泛的...
尽管Apache Commons HttpClient在新的开发中已被推荐使用更现代的Apache HTTP Components替代,但在一些老项目或特定场景下,它仍然是一个实用的工具。然而,需要注意的是,由于其不再更新,可能存在安全漏洞和兼容...
Apache Commons 是一个由 Apache 软件基金会维护的开源项目,它提供了大量的 Java 类库,这些类库包含了许多实用的功能,极大地丰富了 Java 核心库的功能,为开发者提供了更强大的工具集。在Web开发中,Apache ...
Apache Commons是一个非常有用的工具包,解决各种实际的通用问题。(附件中提供了该工具包的jar包,及源文件以供研究) BeanUtils Commons-BeanUtils 提供对 Java 反射和自省API的包装 Betwixt Betwixt提供将 ...
这个压缩包包含了一些 Apache Commons 的核心模块,让我们逐一分析这些 jar 包所代表的知识点。 1. **commons-math-2.2.jar**: 这是 Apache Commons Math 库,提供了一系列数学和统计计算功能,包括线性代数、...
总的来说,Apache Commons Logging是Java开发中一个重要的工具,它提供了一种标准的日志接口,使开发者能够专注于编写代码,而不是处理日志实现的细节。这个压缩包"apache-commons-logging.zip"包含了这个库的组件,...
使用Apache Commons的jar包,可以直接将所需的组件添加到项目类路径中,从而快速集成这些强大的工具。在开发过程中,通过阅读API文档和源代码,可以更高效地利用Apache Commons提供的功能,提高代码质量和可维护性。...
Apache Commons Lang 是一个由 Apache 软件基金会开发的 Java 类库,它是 Apache Commons 项目的一部分,专注于提供一些 Java 核心库中未涵盖的实用工具类。这个压缩包 "apache-commons-lang.jar.zip" 包含了 Apache...
Apache Commons Codec库是Java开发中一个不可或缺的工具包,尤其在处理各种编码问题时,如Base64编码、URL编码、Hex编码以及我们的焦点——MD5编码。这个"org-apache-commons-codec-1.14.zip"压缩包包含的就是Apache...
Apache Commons BeanUtils是Java开发中的一个非常重要的工具包,它属于Apache软件基金会的Commons项目。这个工具包提供了大量方便的API,极大地简化了JavaBean对象之间的属性操作,尤其是在处理复杂的对象模型和数据...
commons-lang3.3.1.jar、Apache Commons包中的一个,包含了一些数据类型工具类,是java.lang.*的扩展。必须使用的jar包。为JRE5.0+的更好的版本所提供 Jar文件包含的类: META-INF/MANIFEST.MFMETA-INF/LICENSE....
org.apache.commons 的 jar 包 org.apache.commons的jar包,Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动,有需要的赶快来CSDN下载吧!
总的来说,Apache Commons Net是Java开发者处理网络任务的强大工具,尤其对于Android开发者来说,它可以弥补原生API的不足,提供了丰富的网络协议支持。通过源代码,开发者可以深入学习网络通信的实现细节,增强自己...
Apache Commons Lang 是一个Java开发库,它提供了许多实用的工具类,增强了Java语言的功能。这个压缩包"apache-commons-lang.zip"包含了Lang项目的源代码,让我们深入了解一下这个库中的核心概念和功能。 首先,...
总的来说,Apache Commons Lang是一个强大的Java工具包,它为日常开发提供了很多便利。无论是在大型企业级应用还是小型项目中,这个库都扮演着不可或缺的角色。开发者可以通过使用它来编写更简洁、更易维护的代码,...
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods....
本站为大家提供了org.apache.commons的jar包下载地址,Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动,需要此类JAR包的朋友们欢迎前来下载使用。 基本简介 commons包,根据...