java web项目常用的工具类——项目经验分享

4 篇文章 0 订阅

数据库连接工具类

 

数据库连接工具类——仅仅获得连接对象 ConnDB.java

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

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

 

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


 

格式转换工具类

 

日期转换工具类 CommUtil.java

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

日期转换类  DateConverter.java

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

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

 

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


 

 

文件操作工具类

 

目录操作工具类 CopyDir.java

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

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

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

目录处理工具类 DealWithDir.java

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

删除文件夹工具类 DeleteFolder.java

 

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


 

文件上传工具类 UploadUtil.java

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

 

其他工具类

MD5编码工具类 MD5Code.java

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


 

读取Config文件工具类 PropertiesConfig.java

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

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

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

邮件发送工具类 SendMail.java

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

分页工具类 SharePager.java

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

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值