StringUtils 类

package com.luceneheritrixbook.util;

import java.security.MessageDigest;

public class StringUtils {

	public static String trim(String line) {
		String result = line.trim();
		while (result.startsWith(" ")) {
			result = result.substring(1);
		}
		while (result.endsWith(" ")) {
			result = result.substring(0, result.length() - 1);
		}

		return result;
	}

	public static String filenameProcess(String name) {
		String result = name.trim();
		result = result.replaceAll("", "_");
		return result;
	}

	/*public static void main(String[] args) {
		String a = "190//180";
		System.out.println(filenameProcess(a));
	}*/
	
/*	public static void main(String[] args)
	{
	//	encodePassword("http://img.pconline.com.cn/images/product/0934/093443/mobile_samsung_f209_1m.jpg","md5");
	//	replace("http:+//img.pconline.com.cn/images/+product/0934/093443/mobile_samsung_f209_1m.jpg","+", " ");
	}
*/


	public static String encodePassword(String password, String algorithm) {
		byte[] unencodedPassword = password.getBytes();
		/*
		 * password为http://img.pconline.com.cn/images/product/0934/093443/mobile_samsung_f209_1m.jpg
		 * unencodePassword【】的值为:
		 * [104, 116, 116, 112, 58, 47, 47, 105, 109, 103, 46, 112, 99, 111, 110, 108, 105, 110,
		 *  101, 46, 99, 111, 109, 46, 99, 110, 47, 105, 109, 97, 103, 101, 115, 47, 112, 114, 
		 *  111, 100, 117, 99, 116, 47, 48, 57, 51, 52, 47, 48, 57, 51, 52, 52, 51, 47, 109, 111, 
		 *  98, 105, 108, 101, 95, 115, 97, 109, 115, 117, 110, 103, 95, 102, 50, 48, 57, 95, 49, 
		 *  109, 46, 106, 112, 103]
		 *  */

		MessageDigest md = null;

		try {
			// first create an instance, given the provider
			md = MessageDigest.getInstance(algorithm);
		} catch (Exception e) {
			return password;
		}

		md.reset();

		// call the update method one or more times
		// (useful when you don't know the size of your data, eg. stream)
		md.update(unencodedPassword);

		// now calculate the hash
		byte[] encodedPassword = md.digest();

		StringBuffer buf = new StringBuffer();

		for (int i = 0; i < encodedPassword.length; i++) {
			if ((encodedPassword[i] & 0xff) < 0x10) {
				buf.append("0");
			}

			buf.append(Long.toString(encodedPassword[i] & 0xff, 16));
		}

		return buf.toString();
	}

	public static final String replace(String line, String oldString,
			String newString) {
		if (line == null) {
			return null;
		}
		int i = 0;
		if ((i = line.indexOf(oldString, i)) >= 0) {
			//indexOf中的i为起始位置,开始搜索oldString的位置,最后i为5
			char[] line2 = line.toCharArray();
			char[] newString2 = newString.toCharArray();
			int oLength = oldString.length();
			//oLength=1,
			StringBuffer buf = new StringBuffer(line2.length);
			//设置缓冲容器的大小为line.length
			buf.append(line2, 0, i).append(newString2);
			i += oLength;
			int j = i;
			while ((i = line.indexOf(oldString, i)) > 0) {
				buf.append(line2, j, i - j).append(newString2);
				i += oLength;
				j = i;
			}
			buf.append(line2, j, line2.length - j);
			//把后面的内容加上去
			return buf.toString();
		}
		return line;
	}

}
调试一遍发觉,除了加密函数有点用处之外,其他trim()及replace()都没什么用,加密方法基本模式都是一致的
注意:首先要在需要设置路径的目录下建立好文件夹,否则会报错(系统无法找到路径)
例如:本例中我们要在c://product//mobile;c://product//image.建好文件夹
OK!
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值