Java常用工具类集合

数据库连接工具类

仅仅获得连接对象 ConnDB.java

  1. package com.util;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. /**
  5. * 数据库连接工具类——仅仅获得连接对象
  6. *
  7. */
  8. public class ConnDB {
  9. private static Connection conn = null;
  10. private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
  11. private static final String URL = "jdbc:mysql://localhost:3306/axt?useUnicode=true&characterEncoding=UTF-8";
  12. private static final String USER_NAME = "root";
  13. private static final String PASSWORD = "root";
  14. public static Connection getConn(){
  15. try {
  16. Class.forName(DRIVER_NAME);
  17. conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. }
  21. return conn;
  22. }
  23. }

数据库连接工具类——包含取得连接和关闭资源 ConnUtil.java

  1. package com.util;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. /**
  8. * @className: ConnUtil.java
  9. * @classDescription: 数据库连接工具类——包含取得连接和关闭资源
  10. * @function:
  11. * @author: Wentasy
  12. * @createTime: 2012-9-24 上午11:51:15
  13. * @modifyTime:
  14. * @modifyReason:
  15. * @since: JDK 1.6
  16. */
  17. public class ConnUtil {
  18. public static final String url = "jdbc:mysql://XXX.XXX.XXX.XXX:3306/dbadapter";
  19. public static final String user = "root";
  20. public static final String password = "XXXXXX";
  21. /**
  22. * 得到连接
  23. * @return
  24. * @throws SQLException
  25. * @throws ClassNotFoundException
  26. */
  27. public static Connection establishConn() throws SQLException,ClassNotFoundException{
  28. Class.forName("com.mysql.jdbc.Driver");
  29. return DriverManager.getConnection(url, user, password);
  30. }
  31. /**
  32. * 关闭连接
  33. * @param conn
  34. * @throws SQLException
  35. */
  36. public static void close(Connection conn) throws SQLException{
  37. if(conn != null){
  38. conn.close();
  39. conn = null;
  40. }
  41. }
  42. /**
  43. * 关闭PreparedStatement
  44. * @param pstmt
  45. * @throws SQLException
  46. */
  47. public static void close(PreparedStatement pstmt) throws SQLException{
  48. if(pstmt != null){
  49. pstmt.close();
  50. pstmt = null;
  51. }
  52. }
  53. /**
  54. * 关闭结果集
  55. * @param rs
  56. * @throws SQLException
  57. */
  58. public static void close(ResultSet rs) throws SQLException{
  59. if(rs != null){
  60. rs.close();
  61. rs = null;
  62. }
  63. }
  64. }

格式转换工具类

日期转换工具类 CommUtil.java

[java] view plaincopy

  1. package com.util;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. /**
  6. * 日期转换工具类
  7. */
  8. public class CommUtil {
  9. /**
  10. * 将日期格式转换成yyyy-MM-dd的字符串格式
  11. * 返回值如:2010-10-06
  12. * @param time 要转换的日期
  13. * @return
  14. */
  15. public static  String dateToString(Date time)  {
  16. SimpleDateFormat formatter = new  SimpleDateFormat ("yyyy-MM-dd"); //定义将日期格式要换成的格式
  17. String stringTime  =  formatter.format(time);
  18. return  stringTime;
  19. }
  20. /**
  21. * 将日期格式转换成yyyyMMdd的字符串格式
  22. * 返回值如:2010-10-06
  23. * @param time 要转换的日期
  24. * @return
  25. */
  26. public static  String dateTimeToString(Date time)  {
  27. SimpleDateFormat formatter = new  SimpleDateFormat ("yyyyMMdd"); //定义将日期格式要换成的格式
  28. String stringTime  =  formatter.format(time);
  29. return  stringTime;
  30. }
  31. /**
  32. * 将日期格式转换成yyyy-MM-dd的字符串格式
  33. * 返回值如:2010-10-06
  34. * @param time 要转换的日期
  35. * @return
  36. */
  37. public static  Date dateToDate(Date time)  {
  38. SimpleDateFormat formatter = new  SimpleDateFormat ("yyyy-MM-dd"); //定义将日期格式要换成的格式
  39. String stringTime  =  formatter.format(time);
  40. Date date = null;
  41. try {
  42. date = formatter.parse(stringTime);
  43. } catch (ParseException e) {
  44. e.printStackTrace();
  45. }
  46. return  date;
  47. }
  48. /**
  49. * 得到当前时间,以字符串表示
  50. * @return
  51. */
  52. public static String getDate(){
  53. Date date = new Date();
  54. return CommUtil.dateToString(date);
  55. }
  56. }

日期转换类  DateConverter.java

[java] view plaincopy

  1. package com.util;
  2. import java.text.DateFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.Map;
  6. import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
  7. /**
  8. * 日期转换类
  9. *
  10. */
  11. public class DateConverter extends DefaultTypeConverter {
  12. private static final DateFormat[] ACCEPT_DATE_FORMATS = {
  13. new SimpleDateFormat("dd/MM/yyyy"),
  14. new SimpleDateFormat("yyyy-MM-dd"),
  15. new SimpleDateFormat("yyyy/MM/dd") }; //支持转换的日期格式
  16. @Override
  17. public Object convertValue(Map context, Object value, Class toType) {
  18. if (toType == Date.class) {  //浏览器向服务器提交时,进行String to Date的转换
  19. Date date = null;
  20. String dateString = null;
  21. String[] params = (String[])value;
  22. dateString = params[0];//获取日期的字符串
  23. for (DateFormat format : ACCEPT_DATE_FORMATS) {
  24. try {
  25. return format.parse(dateString);//遍历日期支持格式,进行转换
  26. } catch(Exception e) {
  27. continue;
  28. }
  29. }
  30. return null;
  31. }
  32. else if (toType == String.class) {   //服务器向浏览器输出时,进行Date to String的类型转换
  33. Date date = (Date)value;
  34. return new SimpleDateFormat("yyyy-MM-dd").format(date);//输出的格式是yyyy-MM-dd
  35. }
  36. return null;
  37. }
  38. }

功能更强大的格式化工具类 FormatUtils.java

[java] view plaincopy

  1. package com.util;
  2. import java.text.DecimalFormat;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. /**
  7. * 功能更强大的格式化工具类
  8. */
  9. public class FormatUtils {
  10. private static SimpleDateFormat second = new SimpleDateFormat(
  11. "yy-MM-dd hh:mm:ss");
  12. private static SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");
  13. private static SimpleDateFormat detailDay = new SimpleDateFormat("yyyy年MM月dd日");
  14. private static SimpleDateFormat fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
  15. private static SimpleDateFormat tempTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  16. private static SimpleDateFormat excelDate = new SimpleDateFormat("yyyy/MM/dd");
  17. /**
  18. * 格式化excel中的时间
  19. * @param date
  20. * @return
  21. */
  22. public static String formatDateForExcelDate(Date date) {
  23. return excelDate.format(date);
  24. }
  25. /**
  26. * 将日期格式化作为文件名
  27. * @param date
  28. * @return
  29. */
  30. public static String formatDateForFileName(Date date) {
  31. return fileName.format(date);
  32. }
  33. /**
  34. * 格式化日期(精确到秒)
  35. *
  36. * @param date
  37. * @return
  38. */
  39. public static String formatDateSecond(Date date) {
  40. return second.format(date);
  41. }
  42. /**
  43. * 格式化日期(精确到秒)
  44. *
  45. * @param date
  46. * @return
  47. */
  48. public static String tempDateSecond(Date date) {
  49. return tempTime.format(date);
  50. }
  51. public static Date tempDateSecond(String str) {
  52. try {
  53. return tempTime.parse(str);
  54. } catch (ParseException e) {
  55. e.printStackTrace();
  56. }
  57. return new Date();
  58. }
  59. /**
  60. * 格式化日期(精确到天)
  61. *
  62. * @param date
  63. * @return
  64. */
  65. public static String formatDateDay(Date date) {
  66. return day.format(date);
  67. }
  68. /**
  69. * 格式化日期(精确到天)
  70. *
  71. * @param date
  72. * @return
  73. */
  74. public static String formatDateDetailDay(Date date) {
  75. return detailDay.format(date);
  76. }
  77. /**
  78. * 将double类型的数字保留两位小数(四舍五入)
  79. *
  80. * @param number
  81. * @return
  82. */
  83. public static String formatNumber(double number) {
  84. DecimalFormat df = new DecimalFormat();
  85. df.applyPattern("#0.00");
  86. return df.format(number);
  87. }
  88. /**
  89. * 将字符串转换成日期
  90. *
  91. * @param date
  92. * @return
  93. * @throws Exception
  94. */
  95. public static Date formateDate(String date) throws Exception {
  96. return day.parse(date);
  97. }
  98. /**
  99. * 将字符日期转换成Date
  100. * @param date
  101. * @return
  102. * @throws Exception
  103. */
  104. public static Date parseStringToDate(String date) throws Exception {
  105. return day.parse(date);
  106. }
  107. public static String formatDoubleNumber(double number) {
  108. DecimalFormat df = new DecimalFormat("#");
  109. return df.format(number);
  110. }
  111. }

文件操作工具类

 

目录操作工具类 CopyDir.java

[java] view plaincopy

  1. package com.util;
  2. import java.io.*;
  3. /**
  4. * 1,建立目的目录。 2,遍历源目录。 3,遍历过程中,创建文件或者文件夹。 原理:其实就是改变了源文件或者目录的目录头。
  5. * @datetime  Dsc  24
  6. */
  7. public class CopyDir {
  8. private File sDir, dDir, newDir;
  9. public CopyDir(String s, String d) {
  10. this(new File(s), new File(d));
  11. }
  12. CopyDir(File sDir, File dDir)// c:\\Test d:\\abc
  13. {
  14. this.sDir = sDir;
  15. this.dDir = dDir;
  16. }
  17. public void copyDir() throws IOException {
  18. // 是创建目的目录。也就是创建要拷贝的源文件夹。Test
  19. // 获取源文件夹名称。
  20. String name = sDir.getName();
  21. // 通过该名称在目的目录创建该文件夹,为了存放源文件夹中的文件或者文件夹。
  22. // 将目的目录和源文件夹名称,封装成File对象。
  23. newDir = dDir;
  24. // new File(dDir,name);
  25. // 调用该对象的mkdir方法。在目的目录创建该文件夹。d:\\abc\\Test
  26. newDir.mkdir();//
  27. // 遍历源文件夹。
  28. listAll(sDir);
  29. }
  30. /*
  31. * 将遍历目录封装成方法。 在遍历过程中,遇到文件创建文件。 遇到目录创建目录。
  32. */
  33. private void listAll(File dir) throws IOException {
  34. File[] files = dir.listFiles();
  35. for (int x = 0; x < files.length; x++) {
  36. if (files[x].isDirectory()) {
  37. createDir(files[x]);// 调用创建目录的方法。
  38. listAll(files[x]);// 在继续进行递归。进入子级目录。
  39. } else {
  40. createFile(files[x]);// 调用创建文件的方法。
  41. }
  42. }
  43. }
  44. /*
  45. * copy目录。通过源目录在目的目录创建新目录。
  46. */
  47. private void createDir(File dir) {
  48. File d = replaceFile(dir);
  49. d.mkdir();
  50. }
  51. /*
  52. * copy文件。
  53. */
  54. private void createFile(File file) throws IOException {
  55. File newFile = replaceFile(file);
  56. // copy文件是一个数据数据传输的过程。需要通过流来完成。
  57. FileInputStream fis = new FileInputStream(file);
  58. FileOutputStream fos = new FileOutputStream(newFile);
  59. byte[] buf = new byte[1024 * 2];
  60. int num = 0;
  61. while ((num = fis.read(buf)) != -1) {
  62. fos.write(buf, 0, num);
  63. }
  64. fos.close();
  65. fis.close();
  66. }
  67. /*
  68. * 替换路径。
  69. */
  70. private File replaceFile(File f) {
  71. // 原理是:将源目录的父目录(C:\\Tset),替换成目的父目录。(d:\\abc\\Test)
  72. String path = f.getAbsolutePath();// 获取源文件或者文件夹的决定路径。
  73. // 将源文件或者文件夹的绝对路径替换成目的路径。
  74. String newPath = path.replace(sDir.getAbsolutePath(), newDir
  75. .getAbsolutePath());
  76. // 将新的目的路径封装成File对象
  77. File newFile = new File(newPath);
  78. return newFile;
  79. }
  80. }

文件/目录部分处理工具类 DealDir.java

[java] view plaincopy

  1. package com.util;
  2. import java.io.File;
  3. import java.util.StringTokenizer;
  4. /**
  5. * 文件/目录 部分处理
  6. * @createTime Dec 25, 2010 7:06:58 AM
  7. * @version 1.0
  8. */
  9. public class DealDir {
  10. /**
  11. * 获取文件的后缀名并转化成大写
  12. *
  13. * @param fileName
  14. *            文件名
  15. * @return
  16. */
  17. public String getFileSuffix(String fileName) throws Exception {
  18. return fileName.substring(fileName.lastIndexOf(".") + 1,
  19. fileName.length()).toUpperCase();
  20. }
  21. /**
  22. * 创建多级目录
  23. *
  24. * @param path
  25. *            目录的绝对路径
  26. */
  27. public void createMultilevelDir(String path) {
  28. try {
  29. StringTokenizer st = new StringTokenizer(path, "/");
  30. String path1 = st.nextToken() + "/";
  31. String path2 = path1;
  32. while (st.hasMoreTokens()) {
  33. path1 = st.nextToken() + "/";
  34. path2 += path1;
  35. File inbox = new File(path2);
  36. if (!inbox.exists())
  37. inbox.mkdir();
  38. }
  39. } catch (Exception e) {
  40. System.out.println("目录创建失败" + e);
  41. e.printStackTrace();
  42. }
  43. }
  44. /**
  45. * 删除文件/目录(递归删除文件/目录)
  46. *
  47. * @param path
  48. *            文件或文件夹的绝对路径
  49. */
  50. public void deleteAll(String dirpath) {
  51. if (dirpath == null) {
  52. System.out.println("目录为空");
  53. } else {
  54. File path = new File(dirpath);
  55. try {
  56. if (!path.exists())
  57. return;// 目录不存在退出
  58. if (path.isFile()) // 如果是文件删除
  59. {
  60. path.delete();
  61. return;
  62. }
  63. File[] files = path.listFiles();// 如果目录中有文件递归删除文件
  64. for (int i = 0; i < files.length; i++) {
  65. deleteAll(files[i].getAbsolutePath());
  66. }
  67. path.delete();
  68. } catch (Exception e) {
  69. System.out.println("文件/目录 删除失败" + e);
  70. e.printStackTrace();
  71. }
  72. }
  73. }
  74. /**
  75. * 文件/目录 重命名
  76. *
  77. * @param oldPath
  78. *            原有路径(绝对路径)
  79. * @param newPath
  80. *            更新路径
  81. * @author lyf 注:不能修改上层次的目录
  82. */
  83. public void renameDir(String oldPath, String newPath) {
  84. File oldFile = new File(oldPath);// 文件或目录
  85. File newFile = new File(newPath);// 文件或目录
  86. try {
  87. boolean success = oldFile.renameTo(newFile);// 重命名
  88. if (!success) {
  89. System.out.println("重命名失败");
  90. } else {
  91. System.out.println("重命名成功");
  92. }
  93. } catch (RuntimeException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. }

目录处理工具类 DealWithDir.java

[java] view plaincopy

  1. package com.util;
  2. import java.io.File;
  3. /**
  4. * 目录处理工具类
  5. *
  6. */
  7. public class DealWithDir {
  8. /**
  9. * 新建目录
  10. */
  11. public static boolean newDir(String path) throws Exception {
  12. File file = new File(path);
  13. return file.mkdirs();//创建目录
  14. }
  15. /**
  16. * 删除目录
  17. */
  18. public static boolean deleteDir(String path) throws Exception {
  19. File file = new File(path);
  20. if (!file.exists())
  21. return false;// 目录不存在退出
  22. if (file.isFile()) // 如果是文件删除
  23. {
  24. file.delete();
  25. return false;
  26. }
  27. File[] files = file.listFiles();// 如果目录中有文件递归删除文件
  28. for (int i = 0; i < files.length; i++) {
  29. deleteDir(files[i].getAbsolutePath());
  30. }
  31. file.delete();
  32. return file.delete();//删除目录
  33. }
  34. /**
  35. * 更新目录
  36. */
  37. public static boolean updateDir(String path, String newPath) throws Exception {
  38. File file = new File(path);
  39. File newFile = new File(newPath);
  40. return file.renameTo(newFile);
  41. }
  42. public static void main(String d[]) throws Exception{
  43. //deleteDir("d:/ff/dddf");
  44. updateDir("D:\\TOOLS\\Tomcat 6.0\\webapps\\BCCCSM\\nationalExperiment/22222", "D:\\TOOLS\\Tomcat 6.0\\webapps\\BCCCSM\\nationalExperiment/224222");
  45. }
  46. }

删除文件夹工具类 DeleteFolder.java

[java] view plaincopy

  1. package com.util;
  2. import java.io.File;
  3. /**
  4. * 删除文件夹
  5. * @createTime DSC 20, 2010 15:38
  6. * @version 2.0
  7. */
  8. public class DeleteFolder {
  9. // 删除文件夹
  10. // param folderPath 文件夹完整绝对路径
  11. public static void delFolder(String folderPath) {
  12. try {
  13. delAllFile(folderPath); // 删除完里面所有内容
  14. String filePath = folderPath;
  15. filePath = filePath.toString();
  16. java.io.File myFilePath = new java.io.File(filePath);
  17. myFilePath.delete(); // 删除空文件夹
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. // 删除指定文件夹下所有文件
  23. // param path 文件夹完整绝对路径
  24. public static boolean delAllFile(String path) {
  25. boolean flag = false;
  26. File file = new File(path);
  27. if (!file.exists()) {
  28. return flag;
  29. }
  30. if (!file.isDirectory()) {
  31. return flag;
  32. }
  33. String[] tempList = file.list();
  34. File temp = null;
  35. for (int i = 0; i < tempList.length; i++) {
  36. if (path.endsWith(File.separator)) {
  37. temp = new File(path + tempList[i]);
  38. } else {
  39. temp = new File(path + File.separator + tempList[i]);
  40. }
  41. if (temp.isFile()) {
  42. temp.delete();
  43. }
  44. if (temp.isDirectory()) {
  45. delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
  46. delFolder(path + "/" + tempList[i]);// 再删除空文件夹
  47. flag = true;
  48. }
  49. }
  50. return flag;
  51. }
  52. }

文件上传工具类 UploadUtil.java

[java] view plaincopy

  1. package com.util;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.util.Calendar;
  10. /**
  11. * 文件上传工具类
  12. *
  13. */
  14. public class UploadUtil {
  15. private static final int BUFFER_SIZE = 16 * 1024;
  16. //保存图片
  17. public static synchronized void copy(File src, File newFile) {
  18. try {
  19. InputStream is = null;
  20. OutputStream os = null;
  21. try {
  22. is = new BufferedInputStream(new FileInputStream(src),
  23. BUFFER_SIZE);
  24. os = new BufferedOutputStream(new FileOutputStream(newFile),
  25. BUFFER_SIZE);
  26. byte[] buffer = new byte[BUFFER_SIZE];
  27. while (is.read(buffer) > 0) {
  28. os.write(buffer);
  29. }
  30. } finally {
  31. if (null != is) {
  32. is.close();
  33. }
  34. if (null != os) {
  35. os.close();
  36. }
  37. }
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. /**
  43. * 返回 年号+月号+天+时+分+秒+随机码
  44. * @return
  45. */
  46. @SuppressWarnings("static-access")
  47. public static synchronized String getTime() {
  48. Calendar calendar = Calendar.getInstance();
  49. String year = calendar.get(calendar.YEAR) + "";
  50. String month = (calendar.get(calendar.MONTH) + 1) + "";
  51. String day = calendar.get(calendar.DAY_OF_MONTH) + "";
  52. String hour = calendar.get(calendar.HOUR_OF_DAY) + "";
  53. String minute = calendar.get(calendar.MINUTE) + "";
  54. String second = calendar.get(calendar.SECOND) + "";
  55. String milliSecond = calendar.get(calendar.MILLISECOND) + "";
  56. int r = (int)(Math.random()*100000);
  57. String random = String.valueOf(r);
  58. return year + month + day + hour + minute + second + milliSecond + random+"a";
  59. }
  60. }

其他工具类

MD5编码工具类 MD5Code.java

[java] view plaincopy

  1. package com.util;
  2. /**
  3. * MD5编码工具类
  4. *
  5. */
  6. public class MD5Code {
  7. static final int S11 = 7;
  8. static final int S12 = 12;
  9. static final int S13 = 17;
  10. static final int S14 = 22;
  11. static final int S21 = 5;
  12. static final int S22 = 9;
  13. static final int S23 = 14;
  14. static final int S24 = 20;
  15. static final int S31 = 4;
  16. static final int S32 = 11;
  17. static final int S33 = 16;
  18. static final int S34 = 23;
  19. static final int S41 = 6;
  20. static final int S42 = 10;
  21. static final int S43 = 15;
  22. static final int S44 = 21;
  23. static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  24. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  26. 0, 0, 0, 0, 0, 0, 0 };
  27. private long[] state = new long[4];// state (ABCD)
  28. private long[] count = new long[2];// number of bits, modulo 2^64 (lsb
  29. // first)
  30. private byte[] buffer = new byte[64]; // input buffer
  31. public String digestHexStr;
  32. private byte[] digest = new byte[16];
  33. public String getMD5ofStr(String inbuf) {
  34. md5Init();
  35. md5Update(inbuf.getBytes(), inbuf.length());
  36. md5Final();
  37. digestHexStr = "";
  38. for (int i = 0; i < 16; i++) {
  39. digestHexStr += byteHEX(digest[i]);
  40. }
  41. return digestHexStr;
  42. }
  43. public MD5Code() {
  44. md5Init();
  45. return;
  46. }
  47. private void md5Init() {
  48. count[0] = 0L;
  49. count[1] = 0L;
  50. // /* Load magic initialization constants.
  51. state[0] = 0x67452301L;
  52. state[1] = 0xefcdab89L;
  53. state[2] = 0x98badcfeL;
  54. state[3] = 0x10325476L;
  55. return;
  56. }
  57. private long F(long x, long y, long z) {
  58. return (x & y) | ((~x) & z);
  59. }
  60. private long G(long x, long y, long z) {
  61. return (x & z) | (y & (~z));
  62. }
  63. private long H(long x, long y, long z) {
  64. return x ^ y ^ z;
  65. }
  66. private long I(long x, long y, long z) {
  67. return y ^ (x | (~z));
  68. }
  69. private long FF(long a, long b, long c, long d, long x, long s, long ac) {
  70. a += F(b, c, d) + x + ac;
  71. a = ((int) a << s) | ((int) a >>> (32 - s));
  72. a += b;
  73. return a;
  74. }
  75. private long GG(long a, long b, long c, long d, long x, long s, long ac) {
  76. a += G(b, c, d) + x + ac;
  77. a = ((int) a << s) | ((int) a >>> (32 - s));
  78. a += b;
  79. return a;
  80. }
  81. private long HH(long a, long b, long c, long d, long x, long s, long ac) {
  82. a += H(b, c, d) + x + ac;
  83. a = ((int) a << s) | ((int) a >>> (32 - s));
  84. a += b;
  85. return a;
  86. }
  87. private long II(long a, long b, long c, long d, long x, long s, long ac) {
  88. a += I(b, c, d) + x + ac;
  89. a = ((int) a << s) | ((int) a >>> (32 - s));
  90. a += b;
  91. return a;
  92. }
  93. private void md5Update(byte[] inbuf, int inputLen) {
  94. int i, index, partLen;
  95. byte[] block = new byte[64];
  96. index = (int) (count[0] >>> 3) & 0x3F;
  97. // /* Update number of bits */
  98. if ((count[0] += (inputLen << 3)) < (inputLen << 3))
  99. count[1]++;
  100. count[1] += (inputLen >>> 29);
  101. partLen = 64 - index;
  102. // Transform as many times as possible.
  103. if (inputLen >= partLen) {
  104. md5Memcpy(buffer, inbuf, index, 0, partLen);
  105. md5Transform(buffer);
  106. for (i = partLen; i + 63 < inputLen; i += 64) {
  107. md5Memcpy(block, inbuf, 0, i, 64);
  108. md5Transform(block);
  109. }
  110. index = 0;
  111. } else
  112. i = 0;
  113. // /* Buffer remaining input */
  114. md5Memcpy(buffer, inbuf, index, i, inputLen - i);
  115. }
  116. private void md5Final() {
  117. byte[] bits = new byte[8];
  118. int index, padLen;
  119. // /* Save number of bits */
  120. Encode(bits, count, 8);
  121. // /* Pad out to 56 mod 64.
  122. index = (int) (count[0] >>> 3) & 0x3f;
  123. padLen = (index < 56) ? (56 - index) : (120 - index);
  124. md5Update(PADDING, padLen);
  125. // /* Append length (before padding) */
  126. md5Update(bits, 8);
  127. // /* Store state in digest */
  128. Encode(digest, state, 16);
  129. }
  130. private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos,
  131. int len) {
  132. int i;
  133. for (i = 0; i < len; i++)
  134. output[outpos + i] = input[inpos + i];
  135. }
  136. private void md5Transform(byte block[]) {
  137. long a = state[0], b = state[1], c = state[2], d = state[3];
  138. long[] x = new long[16];
  139. Decode(x, block, 64);
  140. /* Round 1 */
  141. a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */
  142. d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */
  143. c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */
  144. b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */
  145. a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */
  146. d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */
  147. c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */
  148. b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */
  149. a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */
  150. d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */
  151. c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */
  152. b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */
  153. a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */
  154. d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */
  155. c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */
  156. b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */
  157. /* Round 2 */
  158. a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */
  159. d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */
  160. c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */
  161. b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */
  162. a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */
  163. d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */
  164. c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */
  165. b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */
  166. a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */
  167. d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */
  168. c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */
  169. b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */
  170. a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */
  171. d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */
  172. c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */
  173. b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */
  174. /* Round 3 */
  175. a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */
  176. d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */
  177. c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */
  178. b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */
  179. a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */
  180. d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */
  181. c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */
  182. b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */
  183. a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */
  184. d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */
  185. c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */
  186. b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */
  187. a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */
  188. d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */
  189. c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */
  190. b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */
  191. /* Round 4 */
  192. a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */
  193. d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */
  194. c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */
  195. b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */
  196. a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */
  197. d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */
  198. c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */
  199. b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */
  200. a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */
  201. d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */
  202. c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */
  203. b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */
  204. a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */
  205. d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */
  206. c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */
  207. b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */
  208. state[0] += a;
  209. state[1] += b;
  210. state[2] += c;
  211. state[3] += d;
  212. }
  213. private void Encode(byte[] output, long[] input, int len) {
  214. int i, j;
  215. for (i = 0, j = 0; j < len; i++, j += 4) {
  216. output[j] = (byte) (input[i] & 0xffL);
  217. output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);
  218. output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);
  219. output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);
  220. }
  221. }
  222. private void Decode(long[] output, byte[] input, int len) {
  223. int i, j;
  224. for (i = 0, j = 0; j < len; i++, j += 4)
  225. output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) << 8)
  226. | (b2iu(input[j + 2]) << 16) | (b2iu(input[j + 3]) << 24);
  227. return;
  228. }
  229. public static long b2iu(byte b) {
  230. return b < 0 ? b & 0x7F + 128 : b;
  231. }
  232. public static String byteHEX(byte ib) {
  233. char[] Digit = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘,
  234. ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘ };
  235. char[] ob = new char[2];
  236. ob[0] = Digit[(ib >>> 4) & 0X0F];
  237. ob[1] = Digit[ib & 0X0F];
  238. String s = new String(ob);
  239. return s;
  240. }
  241. }

读取Config文件工具类 PropertiesConfig.java

[java] view plaincopy

  1. package com.util;
  2. import java.io.BufferedInputStream;
  3. import java.io.FileInputStream;
  4. import java.io.InputStream;
  5. import java.util.Properties;
  6. /**
  7. * 读取Config文件工具类
  8. * @version 1.0
  9. * @since JDK 1.6
  10. */
  11. public class PropertiesConfig {
  12. /**
  13. * 获取整个配置文件中的属性
  14. * @param filePath 文件路径,即文件所在包的路径,例如:java/util/config.properties
  15. */
  16. public static Properties readData(String filePath) {
  17. filePath = getRealPath(filePath);
  18. Properties props = new Properties();
  19. try {
  20. InputStream in = new BufferedInputStream(new FileInputStream(filePath));
  21. props.load(in);
  22. in.close();
  23. return props;
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. return null;
  27. }
  28. }
  29. private static String getRealPath(String filePath) {
  30. //获取绝对路径 并截掉路径的”file:/“前缀
  31. return PropertiesConfig.class.getResource("/" + filePath).toString().substring(6);
  32. }
  33. }

自动扫描FTP文件工具类 ScanFtp.java

[java] view plaincopy

  1. package com.util;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. /**
  9. * 自动扫描FTP文件工具类
  10. * 需要定时执行
  11. */
  12. public class ScanFtp {
  13. //服务器图片路径文件夹
  14. private String serverLocal = "D:/TOOLS/Tomcat 6.0/webapps/BCCCSM/modelforcast/";
  15. //图片上传文件夹存放路径,文件夹内应包含AGCM CSM ZS 3个子文件夹分别存放需要扫描到tomcat中的图片
  16. private String saveLocal = "D:/modelForcast/";
  17. /**
  18. * 获得远程权限
  19. * @return
  20. */
  21. private void getFTPAdress(){
  22. //登陆成功
  23. }
  24. /**
  25. * 开始扫描
  26. * @throws IOException
  27. */
  28. private void scan() throws IOException {
  29. this.getFTPAdress();
  30. File file = new File(saveLocal + "AGCM");  //打开AGCM
  31. File[] array = file.listFiles();
  32. String fileName;
  33. File fileTemp;
  34. for(int i = 0; i < array.length; i++){
  35. if(array[i].isFile()) {
  36. fileTemp = array[i];
  37. fileName = fileTemp.getName();//取出文件名
  38. if (!fileName.equals("humbs.db")) {
  39. this.saveFile(fileTemp, 1);//分析每一个文件名字并存储
  40. System.out.println(fileName + " saved");
  41. }
  42. }
  43. }
  44. file = new File(saveLocal + "CSM");  //打开CSM
  45. array = file.listFiles();
  46. for(int i = 0; i < array.length; i++){
  47. if(array[i].isFile()) {
  48. fileTemp = array[i];
  49. fileName = fileTemp.getName();//取出文件名
  50. if (!fileName.equals("humbs.db")) {
  51. this.saveFile(fileTemp, 2);//分析每一个文件名字并存储
  52. System.out.println(fileName + " saved");
  53. }
  54. }
  55. }
  56. file = new File(saveLocal + "ZS");  //打开ZS
  57. array = file.listFiles();
  58. for(int i = 0; i < array.length; i++){
  59. if(array[i].isFile()) {
  60. fileTemp = array[i];
  61. fileName = fileTemp.getName();//取出文件名
  62. if (!fileName.equals("humbs.db")) {
  63. this.saveFile(fileTemp, 3);//分析每一个文件名字并存储
  64. System.out.println(fileName + " saved");
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * 开始执行
  71. * @throws IOException
  72. */
  73. public void execute() throws IOException{
  74. scan();//开始扫描
  75. }
  76. /**
  77. * 按类型存储
  78. * @param file
  79. * @param type
  80. * @throws IOException
  81. */
  82. private void saveFile(File file, int type) throws IOException {
  83. String fileName = file.getName();
  84. //类型A C 和 指数3种
  85. String year = fileName.substring(1, 5);//获得发布年份
  86. String date = fileName.substring(5, 9);//获得发布日期包含月日
  87. String var = null;//获得变量名字
  88. String dir = serverLocal;//存储目录名字
  89. if (type == 1 ) {
  90. var = fileName.substring(11, 15);
  91. dir = dir + "AGCM/" + var + "/" + year + "/" + date;
  92. } else if(type == 2) {
  93. var = fileName.substring(11, 15);
  94. dir = dir + "CSM/" + var + "/" + year + "/" + date;
  95. } else {
  96. var = fileName.substring(11, 15);//指数的暂时没处理
  97. dir = dir + "ZS/" + var + "/" + year + "/" + date;
  98. }
  99. //判断是否存在这样的目录没有就自动创建
  100. File savePath = new File(dir);
  101. if(!savePath.exists()) {
  102. savePath.mkdirs();
  103. }
  104. File saveFile = new File(dir + "/" + fileName);
  105. if(!saveFile.exists()){//如果不存在,就存文件
  106. FileInputStream fis = null;//这里用本地复制暂时代替FTP
  107. FileOutputStream fos =null;
  108. BufferedInputStream bis =null;
  109. BufferedOutputStream bos =null;
  110. int c;
  111. fis = new FileInputStream(file);
  112. bis = new BufferedInputStream(fis);
  113. fos = new FileOutputStream(dir + "/" + fileName);
  114. bos = new BufferedOutputStream(fos);
  115. while((c = bis.read())!= -1)
  116. bos.write(c);
  117. bos.flush();
  118. if(bos != null) bos.close();
  119. if(bis != null) bis.close();
  120. if(fos != null) fos.close();
  121. if(fis != null) fos.close();
  122. } else {
  123. System.out.println("文件已经存在,不进行存储,可清理当前文件.");
  124. }
  125. }
  126. /**
  127. * 测试方法
  128. * @param argv
  129. * @throws IOException
  130. */
  131. public static void main(String argv[])  {
  132. ScanFtp s = new ScanFtp();
  133. try {
  134. s.scan();
  135. } catch (IOException e) {
  136. // TODO Auto-generated catch block
  137. e.printStackTrace();
  138. }
  139. }
  140. }

邮件发送工具类 SendMail.java

[java] view plaincopy

  1. package com.util;
  2. import org.apache.commons.mail.EmailException;
  3. import org.apache.commons.mail.SimpleEmail;
  4. /**
  5. * 邮件发送工具类
  6. */
  7. public class SendMail {
  8. private String hostName;//设置smtp服务器
  9. private String sendMailAddress;//设置发送地址
  10. private String mailPassword;//设置密码
  11. private boolean TLS = false;//设置是否需要TLS登录
  12. private String[] getMailAddress;//设置接收地址s
  13. private String mailTitle;//设置标题
  14. private String mailContent;//设置邮件内容
  15. public  void  send(){
  16. SimpleEmail email = new SimpleEmail();
  17. email.setTLS(TLS); //是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验
  18. email.setHostName(hostName);
  19. try {
  20. email.setFrom(sendMailAddress, sendMailAddress);
  21. email.setAuthentication(sendMailAddress, mailPassword);
  22. email.setCharset("utf-8");//解决中文乱码问题
  23. email.setSubject(mailTitle); //标题
  24. email.setMsg(mailContent);//内容
  25. for(int i = 0; i < getMailAddress.length; ++i){
  26. email.addTo(getMailAddress[i]); //接收方
  27. email.send();
  28. }
  29. } catch (EmailException e) {
  30. //  e.printStackTrace();
  31. }
  32. }
  33. public String getHostName() {
  34. return hostName;
  35. }
  36. public void setHostName(String hostName) {
  37. this.hostName = hostName;
  38. }
  39. public String getSendMailAddress() {
  40. return sendMailAddress;
  41. }
  42. public void setSendMailAddress(String sendMailAddress) {
  43. this.sendMailAddress = sendMailAddress;
  44. }
  45. public String getMailPassword() {
  46. return mailPassword;
  47. }
  48. public void setMailPassword(String mailPassword) {
  49. this.mailPassword = mailPassword;
  50. }
  51. public boolean isTLS() {
  52. return TLS;
  53. }
  54. public void setTLS(boolean tls) {
  55. TLS = tls;
  56. }
  57. public String[] getGetMailAddress() {
  58. return getMailAddress;
  59. }
  60. public void setGetMailAddress(String[] getMailAddress) {
  61. this.getMailAddress = getMailAddress;
  62. }
  63. public String getMailTitle() {
  64. return mailTitle;
  65. }
  66. public void setMailTitle(String mailTitle) {
  67. this.mailTitle = mailTitle;
  68. }
  69. public String getMailContent() {
  70. return mailContent;
  71. }
  72. public void setMailContent(String mailContent) {
  73. this.mailContent = mailContent;
  74. }
  75. }

分页工具类 SharePager.java

[java] view plaincopy

  1. package com.util;
  2. /**
  3. * 分页工具类
  4. *
  5. */
  6. public class SharePager {
  7. private int totalRows; //总行数
  8. private int pageSize = 20; //每页显示的行数
  9. private int currentPage; //当前页号
  10. private int totalPages; //总页数
  11. private int startRow; //当前页在数据库中的起始行
  12. /**
  13. * 默认构造函数
  14. */
  15. public SharePager()
  16. {
  17. }
  18. /**默认每页10行
  19. * @param totalRows
  20. */
  21. public SharePager(int totalRows)
  22. {
  23. this.totalRows = totalRows;
  24. totalPages =(int) Math.ceil((double)totalRows / (double)pageSize);
  25. startRow = 0;
  26. }
  27. /**可自定义每页显示多少页
  28. * @param totalRows
  29. * @param pageSize
  30. */
  31. public SharePager(int totalRows, int pageSize)
  32. {
  33. this.totalRows = totalRows;
  34. this.pageSize = pageSize;
  35. if(this.pageSize<1)
  36. this.pageSize=1;
  37. else if(pageSize>20)
  38. this.pageSize=20;
  39. //        if(this.pageSize>totalRows){
  40. //          this.pageSize=(int)totalRows;
  41. //        }
  42. totalPages =(int) Math.ceil((double)totalRows / (double)this.pageSize);
  43. currentPage = 1;
  44. startRow = 0;
  45. }
  46. /**
  47. * 跳转到首页
  48. */
  49. public void first()
  50. {
  51. this.currentPage = 1;
  52. this.startRow = 0;
  53. }
  54. /**
  55. * 跳转到上一页
  56. */
  57. public void previous()
  58. {
  59. if (currentPage == 1)
  60. {
  61. return;
  62. }
  63. currentPage--;
  64. startRow = (currentPage-1) * pageSize;
  65. }
  66. /**
  67. * 跳转到下一页
  68. */
  69. public void next()
  70. {
  71. if (currentPage < totalPages)
  72. {
  73. currentPage++;
  74. }
  75. startRow = (currentPage-1) * pageSize;
  76. }
  77. /**
  78. *  跳转到尾页
  79. */
  80. public void last()
  81. {
  82. this.currentPage = totalPages;
  83. if(currentPage<1){
  84. currentPage = 1;
  85. }
  86. this.startRow = (currentPage-1) * this.pageSize;
  87. totalPages =(int) Math.ceil((double)totalRows / (double)this.pageSize);
  88. }
  89. /**
  90. * 跳转到指定页
  91. * @param currentPage   指定的页
  92. */
  93. public void refresh(int currentPage)
  94. {
  95. if(currentPage < 0)
  96. {
  97. first();
  98. }
  99. if (currentPage > totalPages)
  100. {
  101. last();
  102. }else{
  103. this.currentPage = currentPage;
  104. this.startRow = (currentPage-1) * this.pageSize;
  105. }
  106. }
  107. public int getStartRow()
  108. {
  109. return startRow;
  110. }
  111. public int getTotalPages()
  112. {
  113. return totalPages;
  114. }
  115. public int getCurrentPage()
  116. {
  117. return currentPage;
  118. }
  119. public int getPageSize()
  120. {
  121. return pageSize;
  122. }
  123. public void setTotalRows(int totalRows)
  124. {
  125. this.totalRows = totalRows;
  126. }
  127. public void setStartRow(int startRow)
  128. {
  129. this.startRow = startRow;
  130. }
  131. public void setTotalPages(int totalPages)
  132. {
  133. this.totalPages = totalPages;
  134. }
  135. public void setCurrentPage(int currentPage)
  136. {
  137. this.currentPage = currentPage;
  138. }
  139. public void setPageSize(int pageSize)
  140. {
  141. this.pageSize = pageSize;
  142. }
  143. public int getTotalRows()
  144. {
  145. return totalRows;
  146. }
  147. }

 
 
 

最新文章

更多

热门文章

更多>>

时间: 2024-08-05 11:09:10

Java常用工具类集合的相关文章

项目经验分享——Java常用工具类集合 转

http://blog.csdn.net/xyw591238/article/details/51678525 写在前面 本文涉及的工具类部分是自己编写,另一部分是在项目里收集的.工具类涉及数据库连接.格式转换.文件操作.发送邮件等等.提高开发效率,欢迎收藏与转载. 数据库连接工具类 数据库连接工具类——仅仅获得连接对象 ConnDB.java [java] package com.util; import java.sql.Connection; import java.sql.DriverM

[转]Java常用工具类集合

转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.util; import java.sql.Connection; import java.sql.DriverManager; /** * 数据库连接工具类——仅仅获得连接对象 * */ public class ConnDB { private static Connection conn = nu

java常用工具类(java技术交流群57388149)

package com.itjh.javaUtil; import java.util.ArrayList; import java.util.List; /** * * String工具类. <br> * * @author 宋立君 * @date 2014年06月24日 */ public class StringUtil { private static final int INDEX_NOT_FOUND = -1; private static final String EMPTY =

java常用工具类(三)—— Excel 操作工具

import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; i

java 常用工具类的使用&lt;三&gt;

1.FtpUtil 1 package com.itjh.javaUtil; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.OutputStream; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import org.apache.commons.net.

java常用工具类(三)—— 文件读取的操作类

定义常用的文件类型 public class FileType { /** * 文件头类型 */ public static final String XML_FILE = "text/xml;charset=UTF-8"; public static final String PDF_FILE = "application/pdf"; public static final String PDG_FILE = "application/x-png&quo

java 常用工具类的使用&lt;二&gt;

一.String工具类 1 package com.mkyong.common; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 /** 7 * 8 * String工具类. <br> 9 * 10 * @author 宋立君 11 * @date 2014年06月24日 12 */ 13 public class StringUtil { 14 15 private static final int INDEX_NOT

java 常用工具类的使用&lt;四&gt;

一.连接数据库的综合类 1 package com.itjh.javaUtil; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.ResultSetMetaData; 8 import java.sql.SQLException; 9 import

Java常用工具类(计算MD5,验证码随机生成,天数差值计算)

写这个博文的目的是为了怕哪天自己的电脑崩溃了,以前写的那些代码就没了,所以将自己写的工具类贴出来,方便以后去使用,也避免自己反复去创造轮子, 也可以对这些方法进行简单修改来完成业务需求,这样就可以极大的提高开发的效率. 方法一:计算字符串的MD5的值 使用方法很简单,直接把值传入方法中就可以了,会返回一个字符串String注意去获取. public final static String calculateMD5(String s) { char hexDigits[] = { '0', '1'