Java工具类
来源:互联网 发布:exe软件代码修改 编辑:程序博客网 时间:2024/06/04 18:03
1 Spring框架,得到ApplicationContext容器的工具类
package com.test.util;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public final class ApplicationContextUtil { private static ApplicationContext ac = null; private ApplicationContextUtil() { } static { ac = new ClassPathXmlApplicationContext("applicationContext.xml"); } public static ApplicationContext getApplicationContext() { return ac; }}
2 MD5加密
import java.security.MessageDigest;public class MD5Tools { public final static String MD5(String pwd) { //用于加密的字符 char md5String[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; try { //使用平台的默认字符集将此 String 编码为 byte序列,并将结果存储到一个新的 byte数组中 byte[] btInput = pwd.getBytes(); //信息摘要是安全的单向哈希函数,它接收任意大小的数据,并输出固定长度的哈希值。 MessageDigest mdInst = MessageDigest.getInstance("MD5"); //MessageDigest对象通过使用 update方法处理数据, 使用指定的byte数组更新摘要 mdInst.update(btInput); // 摘要更新之后,通过调用digest()执行哈希计算,获得密文 byte[] md = mdInst.digest(); // 把密文转换成十六进制的字符串形式 int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { // i = 0 byte byte0 = md[i]; //95 str[k++] = md5String[byte0 >>> 4 & 0xf]; // 5 str[k++] = md5String[byte0 & 0xf]; // F } //返回经过加密后的字符串 return new String(str); } catch (Exception e) { return null; } } }
3 文件上传类
http://blog.csdn.net/u013943420/article/details/71158026
public class MyTools { // 上传头像的方法 [返回你上传的图片名称] public static String uploadHead(HttpServletRequest request, FormFile ff, String id) { String newPhotoName = ""; try { // 考虑给每个用户创建自己的文件夹. String filePath = request.getSession().getServletContext().getRealPath("/"); // filePath就是当前这个web应用是绝对路径(F:\apache-tomcat-6.0.20\webapps\xiaoneinew) InputStream stream = ff.getInputStream();// 把文件读入 String oldPhotoName = ff.getFileName(); newPhotoName = id + oldPhotoName.substring(oldPhotoName.indexOf("."), oldPhotoName.length()); String newFullNewPath = filePath + "\\images\\" + id + "\\head\\"; // 判断newFullNewPath文件夹是否存在 File f = new File(newFullNewPath); if (!f.isDirectory()) { // 不是文件夹,创建即可 f.mkdirs(); } // 把上传的头像名字修改成 编号.后缀 OutputStream bos = new FileOutputStream(newFullNewPath + newPhotoName); int len = 0; byte[] buffer = new byte[8192]; while ((len = stream.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, len);// 将文件写入服务器 } bos.close(); stream.close(); } catch (Exception e) { e.printStackTrace(); } return newPhotoName; } // 上传头像的方法 [返回你上传的图片名称] [我希望保存的相册的图片的路径 // /xiaoneinew/images/用户id/head/albumid/图片] public static String uploadPhoto(HttpServletRequest request, FormFile ff, String uid, String albumId) { String newPhotoName = ""; try { // 考虑给每个用户创建自己的文件夹. String filePath = request.getSession().getServletContext() .getRealPath("/"); // filePath就是当前这个web应用是绝对路径(F:\apache-tomcat-6.0.20\webapps\xiaoneinew) InputStream stream = ff.getInputStream();// 把文件读入 String photoName = ff.getFileName(); String newFullNewPath = filePath + "\\images\\" + uid + "\\album\\" + albumId + "\\"; // 判断newFullNewPath文件夹是否存在 File f = new File(newFullNewPath); if (!f.isDirectory()) { // 不是文件夹,创建即可 f.mkdirs(); } // 把上传的头像名字修成成 编号.后缀 newPhotoName = UUID.randomUUID().toString() + photoName.substring(photoName.indexOf("."), photoName.length()); OutputStream bos = new FileOutputStream(newFullNewPath + newPhotoName); int len = 0; byte[] buffer = new byte[8192]; while ((len = stream.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, len);// 将文件写入服务器 } bos.close(); stream.close(); } catch (Exception e) { e.printStackTrace(); } return newPhotoName; }}
0 0
- java文件工具类
- java日期工具类
- java 绝对路径工具类
- java工具类
- Java 数据处理工具类
- java日期工具类
- java工具类Assert
- 一些Java工具类
- anroid&java工具类
- java StringUtil 工具类
- java身份证号工具类
- java 日期工具类
- Java开发工具类
- java 加密工具类
- java集合工具类
- java时间工具类
- java 时间工具类
- Java加密工具类
- JAVA使用GSON解析JSON数据
- 折半查找的递归实现
- btrfs 入门使用
- 如何申请Coursera课程助学金及获得证书
- 在Android安卓手机多语言输入解决办法
- Java工具类
- 关于enq:TX -allocate ITL entry
- 当file_get_contents或者simplexml_load_file的时候乱码
- C++容器类
- web.鼠标拖拽框
- 框架之Struts2篇----day4.1(事件驱动和模型驱动)
- Eclipse error "build.properties does not exist" 问题解决
- Mysql 设置约束
- Bomb Catcher