时间戳转yyyy-MM-dd HH:mm
public static String timet(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm");
@SuppressWarnings("unused")
long lcc = Long.valueOf(time);
int i = Integer.parseInt(time);
String times = sdr.format(new Date(i * 1000L));
return times;
}
根据URL获取html源码
public static String testGetHtml2(String urlpath) throws Exception {
URL pageUrl = new URL(urlpath);
HttpURLConnection httpUrlConn = (HttpURLConnection) pageUrl
.openConnection();
httpUrlConn.setConnectTimeout(5*1000);
httpUrlConn.setReadTimeout(2*1000);
这里url进行了重定向,如果url没有重定向就直接 httpUrlConn.setRequestMethod("GET")
省略下面几行代码
String location = httpUrlConn.getHeaderField("Location");
pageUrl = new URL(location);
final HttpURLConnection httpUrlConn1 = (HttpURLConnection) pageUrl.openConnection();
httpUrlConn1.setConnectTimeout(5 * 1000);
httpUrlConn1.setReadTimeout(2 * 1000);
httpUrlConn1.setRequestMethod("GET");
int statusCode = httpUrlConn1.getResponseCode();
if (statusCode== 200) {
InputStream inputStream = httpUrlConn1.getInputStream();
byte[] data = readStream(inputStream);
String html = new String(data);
return html;
}else {
return null;
}
}
保存图片到相册
public class SaveImageUtils {
/* *//**
* @param bmp 获取的bitmap数据
* @param picName 自定义的图片名
*/
private static File mPhotoFile = null;
public static void setPhotoFile(File photoFile){
mPhotoFile = photoFile;
}
public static File getPhotoFile(){
return mPhotoFile;
}
/**
* 保存图片到图库
* @param bmp
*/
public static void saveImageToGallery(Bitmap bmp, String bitName ) {
// 首先保存图片
File appDir = new File(Environment.getExternalStorageDirectory(),
"yingtan");
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = bitName + ".jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
setPhotoFile(file);
}
/**
* @param bmp 获取的bitmap数据
* @param picName 自定义的图片名
*/
public static String saveBmp2Gallery(Context context, Bitmap bmp, String picName) {
saveImageToGallery(bmp,picName);
String fileName = null;
//系统相册目录
String galleryPath = Environment.getExternalStorageDirectory()
+ File.separator + Environment.DIRECTORY_DCIM
+ File.separator + "Camera" + File.separator;
// 声明文件对象
File file = null;
// 声明输出流
FileOutputStream outStream = null;
try {
// 如果有目标文件,直接获得文件对象,否则创建一个以filename为名称的文件
file = new File(galleryPath, picName + ".jpg");
// 获得文件相对路径
fileName = file.toString();
// 获得输出流,如果文件中有内容,追加内容
outStream = new FileOutputStream(fileName);
if (null != outStream) {
bmp.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
}
}catch (Exception e) {
e.getStackTrace();
} finally {
try {
if (outStream != null) {
outStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
MediaStore.Images.Media.insertImage(context.getContentResolver(),bmp,fileName,null);
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
context.sendBroadcast(intent);
return "Success";
}
}
调用代码:
SaveImageUtils.saveBmp2Gallery(getContext(), bitmapShow, getTime())
MD5加解密
public class Md5Utils {
/**
* 返回文件的md5值
* @param path
* 要加密的文件的路径
* @return
* 文件的md5值
*/
public static String getFileMD5(String path){
StringBuilder sb = new StringBuilder();
try {
FileInputStream fis = new FileInputStream(new File(path));
//获取MD5加密器
MessageDigest md = MessageDigest.getInstance("md5");
//类似读取文件
byte[] bytes = new byte[10240];//一次读取写入10k
int len = 0;
while((len = fis.read(bytes))!=-1){//从原目的地读取数据
//把数据写到md加密器,类比fos.write(bytes, 0, len);
md.update(bytes, 0, len);
}
//读完整个文件数据,并写到md加密器中
byte[] digest = md.digest();//完成加密,得到md5值,但是是byte类型的。还要做最后的转换
for (byte b : digest) {//遍历字节,把每个字节拼接起来
//把每个字节转换成16进制数
int d = b & 0xff;//只保留后两位数
String herString = Integer.toHexString(d);//把int类型数据转为16进制字符串表示
//如果只有一位,则在前面补0.让其也是两位
if(herString.length()==1){//字节高4位为0
herString = "0"+herString;//拼接字符串,拼成两位表示
}
sb.append(herString);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
/**
* 对传递过来的字符串进行md5加密
* @param str
* 待加密的字符串
* @return
* 字符串Md5加密后的结果
*/
public static String md5(String str){
StringBuilder sb = new StringBuilder();//字符串容器
try {
//获取md5加密器.public static MessageDigest getInstance(String algorithm)返回实现指定摘要算法的 MessageDigest 对象。
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = str.getBytes();//把要加密的字符串转换成字节数组
byte[] digest = md.digest(bytes);//使用指定的 【byte 数组】对摘要进行最后更新,然后完成摘要计算。即完成md5的加密
for (byte b : digest) {
//把每个字节转换成16进制数
int d = b & 0xff;//只保留后两位数
String herString = Integer.toHexString(d);//把int类型数据转为16进制字符串表示
//如果只有一位,则在前面补0.让其也是两位
if(herString.length()==1){//字节高4位为0
herString = "0"+herString;//拼接字符串,拼成两位表示
}
sb.append(herString);
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
}
AES加解密
public class AesEncryptionUtil {
/**
* 算法/模式/填充
**/
private static final String CipherMode = "AES/CBC/PKCS5Padding";
/**
* 创建密钥
**/
private static SecretKeySpec createKey(String key) {
byte[] data = null;
if (key == null) {
key = "";
}
StringBuffer sb = new StringBuffer(16);
sb.append(key);
while (sb.length() < 16) {
sb.append("0");
}
if (sb.length() > 16) {
sb.setLength(16);
}
try {
data = sb.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new SecretKeySpec(data, "AES");
}
private static IvParameterSpec createIV(String password) {
byte[] data = null;
if (password == null) {
password = "";
}
StringBuffer sb = new StringBuffer(16);
sb.append(password);
while (sb.length() < 16) {
sb.append("0");
}
if (sb.length() > 16) {
sb.setLength(16);
}
try {
data = sb.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new IvParameterSpec(data);
}
/**
* 加密字节数据
**/
public static byte[] encrypt(byte[] content, String password, String iv) {
try {
SecretKeySpec key = createKey(password);
Cipher cipher = Cipher.getInstance(CipherMode);
cipher.init(Cipher.ENCRYPT_MODE, key, createIV(iv));
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 加密(结果为16进制字符串)
**/
public static String encrypt(String content, String password, String iv) {
byte[] data = null;
try {
data = content.getBytes("UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
data = encrypt(data, password, iv);
String result = byte2hex(data);
return result;
}
/**
* 解密字节数组
**/
public static byte[] decrypt(byte[] content, String password, String iv) {
try {
SecretKeySpec key = createKey(password);
Cipher cipher = Cipher.getInstance(CipherMode);
cipher.init(Cipher.DECRYPT_MODE, key, createIV(iv));
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解密(输出结果为字符串)
**/
public static String decrypt(String content, String password, String iv) {
byte[] data = null;
try {
data = hex2byte(content);
} catch (Exception e) {
e.printStackTrace();
}
data = decrypt(data, password, iv);
if (data == null)
return null;
String result = null;
try {
result = new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
/**
* 字节数组转成16进制字符串
**/
public static String byte2hex(byte[] b) { // 一个字节的数,
StringBuffer sb = new StringBuffer(b.length * 2);
String tmp = "";
for (int n = 0; n < b.length; n++) {
// 整数转成十六进制表示
tmp = (Integer.toHexString(b[n] & 0XFF));
if (tmp.length() == 1) {
sb.append("0");
}
sb.append(tmp);
}
return sb.toString().toUpperCase(); // 转成大写
}
/**
* 将hex字符串转换成字节数组
**/
private static byte[] hex2byte(String inputString) {
if (inputString == null || inputString.length() < 2) {
return new byte[0];
}
inputString = inputString.toLowerCase();
int l = inputString.length() / 2;
byte[] result = new byte[l];
for (int i = 0; i < l; ++i) {
String tmp = inputString.substring(2 * i, 2 * i + 2);
result[i] = (byte) (Integer.parseInt(tmp, 16) & 0xFF);
}
return result;
}
}