/**
* 生成指定长度的随机数
*
* @param length
* @return
*/
public static int genRandomNum(int length) {
int num = 1;
double random = Math.random();
if (random < 0.1) {
random = random + 0.1;
}
for (int i = 0; i < length; i++) {
num = num * 10;
}
return (int) ((random * num));
}
/**
* 判断是否为空字符串
* @param str
* @return 如果为空,则返回true
*/
public static boolean isEmpty(String str){
return str == null || str.trim().length() == 0;
}
/**
* 判断collection是否为空
* @param collection
* @return 如果为空,则返回true
*/
public static boolean isEmpty(Collection> collection){
//return CollectionUtils.isEmpty(collection);
return collection == null || collection.isEmpty();
}
/**
* 判断map是否为空
* @param map
* @return
*/
public static boolean isEmpty(Map,?> map){
return map == null || map.isEmpty();
}
/**
* 判断数组是否非空
* @param array
* @return
*/
public static boolean isEmpty(Object[] array){
return array==null||array.length==0;
}
/**
* 获取当前时间
* @param
* @return yyyy-MM-dd
*/
public static String getDate() {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String Now = sdf.format(d);
return Now;
}
/**
* 获取当前时间(适用于生成水流)
* @param
* @return yyyy-MM-dd HH:mm:ss
*/
public static String getTime() {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String time = sdf.format(d);
return time;
}
/**
* 获取当前时间
* @param
* @return yyyy-MM-dd HH:mm:ss
*/
public static String getNow() {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(d);
return time;
}
/**
*
* @方法名:encryption
* @方法描述:MD5加密32位小写
* @param encryptStr 明文
* @return 32位小写密文
*/
public static String encryption(String encryptStr) {
String m_algorithm = "MD5";
String re_md5 = new String();
try {
MessageDigest md = MessageDigest.getInstance(m_algorithm);
md.update(encryptStr.getBytes("UTF-8"));
byte b[] = md.digest();
int i;
StringBuilder builder = new StringBuilder("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
builder.append("0");
builder.append(Integer.toHexString(i));
}
re_md5 = builder.toString();
}
catch (Exception e) {
e.printStackTrace();
}
return re_md5;
}
/**
*
* @方法名:ENCRYPTION
* @方法描述:MD5加密32位大写
* @param encryptStr 明文
* @return 32位大写密文
*/
public static String ENCRYPTION(String encryptStr) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
try {
byte[] btInput = encryptStr.getBytes();
// 获得MD5摘要算法的 MessageDigest 对象
MessageDigest mdInst = MessageDigest.getInstance("MD5");
// 使用指定的字节更新摘要
mdInst.update(btInput);
// 获得密文
byte[] md = mdInst.digest();
// 把密文转换成十六进制的字符串形式
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
}
catch (Exception e) {
return null;
}
}
/**
*
* @方法名:encryption16
* @方法描述:MD5加密16位小写
* @param encryptStr 明文
* @return 16位小写密文
*/
public static String encryption16(String encryptStr) {
return encryption(encryptStr).substring(8, 24);
}
/**
*
* @方法名:ENCRYPTION16
* @方法描述: MD5加密16位大写
* @param encryptStr 明文
* @return 16位大写密文
*/
public static String ENCRYPTION16(String encryptStr) {
return ENCRYPTION(encryptStr).substring(8, 24);
}
/**
public static String formatMoney2Str(float money) {
java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
return df.format(money);
}
/**
一维条形码类
@data 2019-12-23
@author Gaara
/
public class barCodeUtil {
/*
*一维条形码
}
public static void main(String[] args) {
String msg = “My name is Gaara”;
String path = “E:/barCode/barCode.png”;
barCodeUtil.getBarCode(msg,path);
}
}
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class QRCodeUtil {
private static final String CHARSET = “utf-8”;
private static final String FORMAT_NAME = “JPG”;
// 二维码尺寸
private static final int QRCODE_SIZE = 300;
// LOGO宽度
private static final int WIDTH = 60;
// LOGO高度
private static final int HEIGHT = 60;
@SuppressWarnings({ "unchecked", "rawtypes" })
private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
if (imgPath == null || "".equals(imgPath)) {
return image;
}
// 插入图片
QRCodeUtil.insertImage(image, imgPath, needCompress);
return image;
}
private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception {
File file = new File(imgPath);
if (!file.exists()) {
System.err.println("" + imgPath + " 该文件不存在!");
return;
}
Image src = ImageIO.read(new File(imgPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) { // 压缩LOGO
if (width > WIDTH) {
width = WIDTH;
}
if (height > HEIGHT) {
height = HEIGHT;
}
Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
src = image;
}
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
public static void encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
mkdirs(destPath);
// String file = new Random().nextInt(99999999)+".jpg";
// ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));
ImageIO.write(image, FORMAT_NAME, new File(destPath));
}
public static BufferedImage encode(String content, String imgPath, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
return image;
}
public static void mkdirs(String destPath) {
File file = new File(destPath);
// 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
}
public static void encode(String content, String imgPath, String destPath) throws Exception {
QRCodeUtil.encode(content, imgPath, destPath, false);
}
// 被注释的方法
/*
* public static void encode(String content, String destPath, boolean
* needCompress) throws Exception { QRCodeUtil.encode(content, null, destPath,
* needCompress); }
*/
public static void encode(String content, String destPath) throws Exception {
QRCodeUtil.encode(content, null, destPath, false);
}
public static void encode(String content, String imgPath, OutputStream output, boolean needCompress)
throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
ImageIO.write(image, FORMAT_NAME, output);
}
public static void encode(String content, OutputStream output) throws Exception {
QRCodeUtil.encode(content, null, output, false);
}
public static String decode(File file) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}
public static String decode(String path) throws Exception {
return QRCodeUtil.decode(new File(path));
}
package com.gaara.utils;
public class QRCodeTest {
public static void main(String[] args) throws Exception {
// 存放在二维码中的内容
String text = "MOM系统二维码";
// 嵌入二维码的图片路径
String imgPath = "E:/qrCode/MOM.jpg";
// 生成的二维码的路径及名称
String destPath = "E:/qrCode/qrcode/pic1.jpg";
//生成二维码
QRCodeUtil.encode(text, imgPath, destPath, true);
// 解析二维码
String str = QRCodeUtil.decode(destPath);
// 打印出解析出的内容
System.out.println("二维码解析的内容为:"+"\n" +str);
}
}
package com.taotao.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
/**
ftp上传下载工具类
Title: FtpUtil
Description:
Company: www.itcast.com
@author Gaara
@date 2015年7月29日下午8:11:51
@version 1.0
*/
public class FtpUtil {
/**
/**
Description: 从FTP服务器下载文件
@param host FTP服务器hostname
@param port FTP服务器端口
@param username FTP登录账号
@param password FTP登录密码
@param remotePath FTP服务器上的相对路径
@param fileName 要下载的文件名
@param localPath 下载后保存到本地的路径
@return
*/
public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
String fileName, String localPath) {
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(host, port);
// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + “/” + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
public static void main(String[] args) {
try {
FileInputStream in=new FileInputStream(new File(“D:\temp\image\gaigeming.jpg”));
boolean flag = uploadFile(“192.168.25.133”, 21, “ftpuser”, “ftpuser”, “/home/ftpuser/www/images”,"/2015/01/21", “gaigeming.jpg”, in);
System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}