java工具类utils

String 转换 Date

private Date dateUtils(String date) {
        try {
            //字符串格式
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            return sDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

随机id

package com.store.utils;

import java.util.UUID;

public class UUIDUtils {
	/**
	 * 随机生成id
	 * @return
	 */
	public static String getId(){
		return UUID.randomUUID().toString().replace("-", "").toUpperCase();
	}
	
	/**
	 * 生成随机码
	 * @return
	 */
	public static String getCode(){
		return getId();
	}
	
	public static void main(String[] args) {
		System.out.println(getId());
	}
}

图片复制与删除

package com.store.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;

public class LioUtils {
	/**
	 * 储存图片 --图片二进制 -- 路径+name
	 * 
	 * @param pimageName
	 * @param is
	 * @param path
	 * @throws IOException
	 */
	public static void myLioUtils( InputStream is, String path) throws IOException {
		// TODO Auto-generated method stub

		// 在服务端创建空文件
		File file = new File(path);

		if (!file.exists()) {
			file.createNewFile();
		}

		// 建立和空文件对应的输入流
		OutputStream os = new FileOutputStream(file);
		
		IOUtils.copy(is, os);
		IOUtils.closeQuietly(os);
		IOUtils.closeQuietly(is);
	}

	/**
	 * 删除图片 -- 图片路径+图片名
	 * 
	 * @param fileName
	 * @return
	 */
	public static boolean deleteFile(String fileName) {
		File file = new File(fileName);
		// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
		if (file.exists() && file.isFile()) {
			if (file.delete()) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

}

寻找指定cookie

package com.store.utils;

import javax.servlet.http.Cookie;

public class MyCookieUtils {

	public static Cookie getcookie(String st, Cookie[] cookies) {
		// TODO Auto-generated method stub
		if (st == null) {
			return null;
		}
		if (cookies == null) {
			return null;
		}
		for (Cookie co : cookies) {
			if (st.equals(co.getName())) {
				return co;
			}
		}

		return null;
	}

}

MD5

public static String encryptMd5(String inStr) {

        MessageDigest md = null;
        String out = null;
        try {
            md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(inStr.getBytes());
            return new String(Base64.encodeBase64(digest));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();

        }

        return null;
    }

上传DFS图片

判断url

/**
	 * 判断字符串是否为URL
	 * 
	 * @param urls 需要判断的String类型url
	 * @return true:是URL;false:不是URL
	 */
	public boolean isHttpUrl(String urls) {
		boolean isurl = false;
		String regex = "(((https|http)?://)?([a-z0-9]+[.])|(www.))"
				+ "\\w+[.|\\/]([a-z0-9]{0,})?[[.]([a-z0-9]{0,})]+((/[\\S&&[^,;\u4E00-\u9FA5]]+)+)?([.][a-z0-9]{0,}+|/?)";// 设置正则表达式

		Pattern pat = Pattern.compile(regex.trim());// 对比
		Matcher mat = pat.matcher(urls.trim());
		isurl = mat.matches();// 判断是否匹配
		if (isurl) {
			isurl = true;
		}
		return isurl;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值