MD5 加密
public class MD5Util {
private MD5Util(){
}
public static String encode(String pass) throws NoSuchAlgorithmException {
//获得加密对象
MessageDigest md5 = MessageDigest.getInstance("MD5");
//传入(更新)需要加密的数据
byte[] sourcepass = pass.getBytes();
md5.update(sourcepass);
//开始加密(结果是字节数组)
byte[] digest = md5.digest();
System.out.println("加密成功");
//转换成数字(第一个参数是符号)
String s = new BigInteger(1, digest).toString(16).toUpperCase();
return s;
}
}
文件上传及路径处理
public class FileUtil {
//src/upload/user/curDate/UUID.原文件后缀
private FileUtil(){}
private static final String p = "src/upload/user/";
public static String uploadImag(String path){
//时间戳文件路径
String s = LocalDate.now().toString();
File f = new File(p,s);
if(!f.exists())
{
f.mkdirs();
}
//UUID文件名
String fn = UUID.randomUUID().toString().replaceAll("-", "");
//获取源文件后缀名
String hzm = path.substring(path.lastIndexOf("."));
String targetFilename = f.getPath()+"/"+fn+hzm;
try {
FileInputStream fs = new FileInputStream(path);
FileOutputStream fo = new FileOutputStream(targetFilename);
byte[] b = new byte[1024];
int len = fs.read(b);
while(len!=-1)
{
fo.write(b,0,len);
len = fs.read(b);
}
} catch (IOException e) {
e.printStackTrace();
}
return targetFilename;
}
}