Skip to content

Latest commit

 

History

History
244 lines (214 loc) · 6.56 KB

详细说明.md

File metadata and controls

244 lines (214 loc) · 6.56 KB

##springmore-commons 这是一个工具类库 包含如下功能:

FTPUtil

FTPUtil详细文档

SFTPUtil

SFTPUtil详细文档

org.springmore.commons.codec:

  • Base64.java Base64编码与解码

org.springmore.commons.io:

  • ExcelUtil excel文件读写
  • FileUtil 文件读写
  • FTPUtil ftp操作
  • ResourceUtil
  • XMLUtil dom4j jaxb封装
  • ImageUtil 图片缩放,切割封装

org.springmore.commons.lang

  • ArrayUtil
  • HexUtil 字符字节十六进制转换
  • StringUtil
  • DateUtil

org.springmore.commons.security

  • DESedeUtil 3des加密
  • DESUtil 单des加密
  • Md5Util md5加密
  • RSAUtil rsa加密

org.springmore.commons.web

  • HttpClientUtil http https封装
  • WebUtil servlet发送response信息封装,发送json字符串封装

FileUtil

继承了apache commons FileUtils的所有功能 文件复制

File srcDir = new File("srcDir");
File destDir = new File("destDir");
FileUtil.copyDirectory(srcDir, destDir);

创建目录

File dir = new File("dir");
FileUtil.forceMkdir(dir);

将文件中的内容读出来,并封装到list中

File file = new File("file");
List<String> readLines = FileUtil.readLines(file);

HttpClientUtil

功能:http以及https 基于最新的httpcomponents包实现 get请求,返回String报文,返回的报文默认是UTF-8编码 如果需要制定编码,可传入编码参数

@Test
public void testDoGetStringString() throws Exception {
	String doGet = HttpClientUtil.get("http://localhost:8888/login/");
	String doGet2 = HttpClientUtil.get("http://localhost:8888/login/",HttpClientUtil.UTF_8);
	System.out.println(doGet);
}	

post请求,可以制定编码

@Test
public void testDoPost() throws Exception {
	List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("user.userName", "哈哈"));
	String doGet = HttpClientUtil.post("http://localhost:8888/login/login!login.ac",nvps);
	System.out.println(doGet);
}

ssl请求

@Test
public void testSSL() throws Exception {
	String doGet = HttpClientUtil.getSSL("https://www.baidu.com", "utf-8");
	List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("user.userName", "哈哈"));
	String post = HttpClientUtil.postSSL("https://www.baidu.com", nvps,"utf-8");
	System.out.println(post);
}	

文件上传和下载

@Test
public void getFile() throws Exception{
	HttpClientUtil.getFile("http://localhost:8888/login/login!login.ac");
	HttpClientUtil.postFile("http://localhost:8888/login/login!login.ac", "fileName", new File("d:/test.txt"));
}

##ImageUtils 图片缩放,等比压缩图片

@Test
public void 按比例缩放() throws IOException {
	File src = new File("C:\\Users\\bypay\\Desktop\\智派平台入件资料\\a1.jpg");
	File dest = new File("C:\\Users\\bypay\\Desktop\\智派平台入件资料\\a2.jpg");
	ImageUtils.scale(src, dest, 10, true);
}

/**
 * 不按比例
 * @author 唐延波
 * @date 2015-6-12
 * @throws IOException
 */
@Test
public void 按宽度和高度缩放() throws IOException {
	File src = new File("C:\\Users\\bypay\\Desktop\\智派平台入件资料\\a1.jpg");
	File dest = new File("C:\\Users\\bypay\\Desktop\\智派平台入件资料\\a2.jpg");
	ImageUtils.scale(src, dest, 1000,1000,true);
}

@Test
public void 按宽度压缩() {
	File src = new File("C:\\Users\\bypay\\Desktop\\智派平台入件资料\\a.jpg");
	File dest = new File("C:\\Users\\bypay\\Desktop\\智派平台入件资料\\a2.jpg");
	ImageUtils.scaleByWidth(src, dest, 1200);
}

@Test
public void 图片切割() {
	File src = new File("C:\\Users\\bypay\\Desktop\\智派平台入件资料\\a.jpg");
	File dest = new File("C:\\Users\\bypay\\Desktop\\智派平台入件资料\\a3.jpg");
	ImageUtils.cut(src, dest, 0, 0, 3000, 3000);
}

DesUtil示例

public class DESUtilTest {

	/**
	 * 加密后的数据hex为dbeffec5df17927a
	 * 
	 * @param args
	 * @throws Exception
	 * @author 唐延波
	 * @date 2015-6-9
	 */
	@Test
	public void testEncryptByteArrayByteArray() throws Exception {
		// byte[] key = initSecretKey();
		byte[] key = "12345678".getBytes("utf-8");
		System.out.println("key:" + showByteArray(key));
		System.out.println("key hex:" + Hex.encodeHexString(key));

		String data = "11111111"; // "DESxxx";
		System.out.println("加密前数据: string:" + data);
		System.out.println("加密前数据: byte[]:"
				+ showByteArray(data.getBytes("utf-8")));
		System.out.println("加密前数据: hex:"
				+ Hex.encodeHexString(data.getBytes("utf-8")));
		System.out.println();
		byte[] encryptData = DESUtil.encrypt(data.getBytes(), key);
		System.out.println("加密后数据: byte[]:" + showByteArray(encryptData));
		System.out.println("加密后数据: hexStr:" + Hex.encodeHexString(encryptData));

		String encodeBase64String = Base64.encodeBase64String(encryptData);
		System.out.println("加密后数据: base64:" + encodeBase64String);

		System.out.println();
		byte[] decryptData = DESUtil.decrypt(encryptData, key);
		System.out.println("解密后数据: byte[]:" + showByteArray(decryptData));
		System.out.println("解密后数据: string:" + new String(decryptData));
	}

	private static String showByteArray(byte[] data) {
		if (null == data) {
			return null;
		}
		StringBuilder sb = new StringBuilder("{");
		for (byte b : data) {
			sb.append(b).append(",");
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("}");
		return sb.toString();
	}
}

XMLUtil示例

@Test
public void testXmlToObj() throws JAXBException {
	String xml = "<user>" +
			"<id>1</id>" +
			"<name>22</name>" +
			"</user>";
	User xmlToObj = XMLUtil.XmlToObj(xml, User.class);
	xmlToObj.toString();
	//System.out.println(xmlToObj);
}

DateUtil示例

@Test
public void testParseDate() throws ParseException {
	Date parseDate1 = DateUtil.parseDate("20150609134055", "yyyyMMddHHmmss");
	String date = DateUtil.format(parseDate1, "yyyyMMddHHmmss");
	System.out.println(date);
	System.out.println(parseDate1);
	
	String date2 = DateUtil.format(new Date(), "yyMMdd");
	System.out.println(date2);
	
}

StringUtil示例

@Test
public void testIsEmpty() {
	Assert.assertTrue(StringUtil.isEmpty(""));
	Assert.assertTrue(StringUtil.isEmpty(null));
	Assert.assertTrue(StringUtil.isBlank(" "));
	Assert.assertTrue(StringUtil.isBlank(""));
	Assert.assertTrue(StringUtil.isBlank(null));
	StringUtils.isEmpty("");
	Assert.assertTrue(StringUtil.isNotEmpty(" "));
	
}

@Test
public void testJoin() {
	String join = StringUtil.join("111", "211");
	System.out.println(join);
	
	String reverse = StringUtil.reverse("abc");
	
}