(PropertiesUtil、MD5加密對檔案加密、FileUtil、DateTime)工具類
PropertiesUtil類 --獲取某檔案的屬性值public class PropertiesUtil {
private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
private static Properties props;
static { String fileName = "mmall.properties";
props = new Properties();
try {
props
MD5加密工具類(需要上面的工具類)
public class MD5Util { private static String byteArrayToHexString(byte b[]) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) resultSb.append(byteToHexString(b[i])); return resultSb.toString(); } private static String byteToHexString(byte b) { int n = b; if (n < 0) n += 256; int d1 = n / 16; int d2 = n % 16; return hexDigits[d1] + hexDigits[d2]; } /** * 返回大寫MD5 * * @param origin* @param charsetname* @return*/private static String MD5Encode(String origin, String charsetname) { String resultString = null; try { resultString = new String(origin); MessageDigest md = MessageDigest.getInstance("MD5"); if (charsetname == null || "".equals(charsetname)) resultString = byteArrayToHexString(md.digest(resultString.getBytes())); elseresultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname))); } catch (Exception exception) { } return resultString.toUpperCase(); } public static String MD5EncodeUtf8(String origin) { origin = origin + PropertiesUtil.getProperty("password.salt", ""); return MD5Encode(origin, "utf-8"); } private static final String hexDigits[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};}
也可以對檔案內容進行加密 在上面類裡新增下面方法
呼叫為 MD5Util MD5Util.getFileMD5("d:\\file\\新建文字文件.txt")
/**
* 獲取檔案的MD5碼
*
* @param absPath
* 檔案路徑
* @return 檔案的MD5碼
*/
public final static String getFileMD5(String absPath) {
try {
File file = new File(absPath);
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
FileInputStream fis = new FileInputStream(file);
FileChannel filechannel = fis.getChannel();//通道
MappedByteBuffer mbb = filechannel
.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
mdTemp.update(mbb);
byte[] md = mdTemp.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];
}
fis.close();
return new String(str);
} catch (Exception e) {
return "";
}
}
FileUtil類 --儲存、刪除檔案 拼接檔名
public class FileUtil {
/**
* 將MultipartFile儲存到指定的路徑下
*
* @param file Spring的MultipartFile物件
* @param savePath 儲存路徑
* @return 儲存的檔名,當返回NULL時為儲存失敗。
* @throws IOException
* @throws IllegalStateException
*/
public static String save(MultipartFile file,String savePath) throws IllegalStateException, IOException {
if (file != null && file.getSize() > 0) {
File fileFolder = new File(savePath);
if (!fileFolder.exists()) {
fileFolder.mkdirs();
}
File saveFile = getFile(savePath, file.getOriginalFilename());
file.transferTo(saveFile);
return saveFile.getName();
}
return null;
}
/**
* 刪除檔案
*
* @param filePath
* 檔案路徑
* @return 是否刪除成功:true-刪除成功,false-刪除失敗
*/
public static boolean delete(String filePath) {
File file = new File(filePath);
if (file.isFile()) {
file.delete();
return true;
}
return false;
}
/**
* 拼接檔案
* */
private static File getFile(String savePath, String originalFilename) {
//UUID.randomUUID().toString()生成16位數字
//String fileName =UUID.randomUUID().toString() + "_" + originalFilename;
String fileName = System.currentTimeMillis() + "_" + originalFilename;
File file = new File(savePath + fileName);
if (file.exists()) {
return getFile(savePath, originalFilename);
}
return file;
}
}
DateTime工具類 --String和Date之間的轉換
public class DateTimeUtil { //joda-time //str->Date //Date->strpublic static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss"; public static Date strToDate(String dateTimeStr, String formatStr){ DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr); DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr); return dateTime.toDate(); } public static String dateToStr(Date date,String formatStr){ if(date == null){ return StringUtils.EMPTY; } DateTime dateTime = new DateTime(date); return dateTime.toString(formatStr); } public static Date strToDate(String dateTimeStr){ DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT); DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr); return dateTime.toDate(); } public static String dateToStr(Date date){ if(date == null){ return StringUtils.EMPTY; } DateTime dateTime = new DateTime(date); return dateTime.toString(STANDARD_FORMAT); } /*public static void main(String[] args) { System.out.println(DateTimeUtil.dateToStr(new Date(),"yyyy-MM-dd HH:mm:ss")); System.out.println(DateTimeUtil.strToDate("2010-01-01 11:11:11","yyyy-MM-dd HH:mm:ss")); }*/}