init
This commit is contained in:
25
op-tools/pom.xml
Normal file
25
op-tools/pom.xml
Normal file
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>OpenPark</artifactId>
|
||||
<groupId>cn.feast.coding</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<packaging>jar</packaging>
|
||||
<artifactId>op-tools</artifactId>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.feast.coding</groupId>
|
||||
<artifactId>op-model</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,225 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
import cn.feast.coding.model.generator.Colunm;
|
||||
import cn.feast.coding.model.generator.DataSource;
|
||||
import cn.feast.coding.model.generator.Table;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class AlibabaDbConnect {
|
||||
private static DruidDataSource dataSource = null;
|
||||
|
||||
/**
|
||||
* 构造函数完成数据库的连接和连接对象的生成
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public AlibabaDbConnect() {
|
||||
|
||||
}
|
||||
|
||||
public static void GetDbConnect(DataSource data) throws Exception {
|
||||
try {
|
||||
if (dataSource == null) {
|
||||
dataSource = new DruidDataSource();
|
||||
//设置连接参数
|
||||
dataSource.setUrl(data.getJdbcUrl());
|
||||
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
|
||||
dataSource.setUsername(data.getJdbcUser());
|
||||
dataSource.setPassword(data.getJdbcPass());
|
||||
//配置初始化大小、最小、最大
|
||||
dataSource.setInitialSize(1);
|
||||
dataSource.setMinIdle(1);
|
||||
dataSource.setMaxActive(20);
|
||||
//连接泄漏监测
|
||||
dataSource.setRemoveAbandoned(true);
|
||||
dataSource.setRemoveAbandonedTimeout(30);
|
||||
//配置获取连接等待超时的时间
|
||||
dataSource.setMaxWait(20000);
|
||||
//配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
|
||||
dataSource.setTimeBetweenEvictionRunsMillis(20000);
|
||||
//防止过期
|
||||
dataSource.setValidationQuery("SELECT 'x'");
|
||||
dataSource.setTestWhileIdle(true);
|
||||
dataSource.setTestOnBorrow(true);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据库所有数据表名称
|
||||
*
|
||||
* @param dataSource
|
||||
* @return
|
||||
*/
|
||||
public static List<Table> getTables(DataSource dataSource) {
|
||||
List<Table> tables = new ArrayList<Table>();
|
||||
Table table = null;
|
||||
Connection conn = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = getConnect(dataSource);
|
||||
DatabaseMetaData dbMetaData = conn.getMetaData();
|
||||
rs = dbMetaData.getTables(null, null, null, new String[]{"TABLE"});
|
||||
while (rs.next()) {
|
||||
table = new Table();
|
||||
String tableName = rs.getString("TABLE_NAME");
|
||||
table.setTableName(tableName);
|
||||
table.setClassName(getClassName(tableName));
|
||||
tables.add(table);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取表数据
|
||||
*
|
||||
* @param dataSource 数据源
|
||||
* @param tableName 表名
|
||||
* @return
|
||||
*/
|
||||
public static List<Colunm> readData(DataSource dataSource,
|
||||
String tableName) {
|
||||
List<Colunm> properties = new ArrayList<Colunm>();
|
||||
Colunm colunm = null;
|
||||
Connection conn = null;
|
||||
ResultSet rs = null;
|
||||
String primaryKey = genFieldName(getPrimaryKey(dataSource, tableName));
|
||||
try {
|
||||
conn = getConnect(dataSource);
|
||||
DatabaseMetaData dbmd = conn.getMetaData();
|
||||
rs = dbmd.getColumns(null, null, tableName, null);
|
||||
while (rs.next()) {
|
||||
colunm = new Colunm();
|
||||
String colunmName = genFieldName(rs.getString("COLUMN_NAME"));
|
||||
colunm.setColunmName(colunmName);
|
||||
colunm.setFiledName(rs.getString("COLUMN_NAME"));
|
||||
if (colunmName.equals(primaryKey)) {
|
||||
colunm.setIsPrimary(1);
|
||||
} else {
|
||||
colunm.setIsPrimary(0);
|
||||
}
|
||||
int dataType = rs.getInt("DATA_TYPE"); //对应的java.sql.Types类型
|
||||
int columnSize = rs.getInt("COLUMN_SIZE");//列大小
|
||||
int decimalDigits = rs.getInt("DECIMAL_DIGITS");//小数位数
|
||||
colunm.setColunmType(JavaType.getType(dataType, columnSize, decimalDigits));
|
||||
colunm.setShowName(rs.getString("REMARKS"));
|
||||
properties.add(colunm);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据表主键
|
||||
*
|
||||
* @param dataSource
|
||||
* @param tableName
|
||||
* @return
|
||||
*/
|
||||
public static String getPrimaryKey(DataSource dataSource, String tableName) {
|
||||
Connection conn = null;
|
||||
ResultSet rs = null;
|
||||
String primaryKeyName = "";
|
||||
try {
|
||||
conn = getConnect(dataSource);
|
||||
rs = conn.getMetaData().getPrimaryKeys(null, null, tableName);
|
||||
if (rs.next()) {
|
||||
primaryKeyName = rs.getString(4);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (StringUtils.isNotEmpty(primaryKeyName)) {
|
||||
return primaryKeyName;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据表字段名获取java中的字段名
|
||||
*
|
||||
* @param field 字段名
|
||||
* @return
|
||||
*/
|
||||
public static String genFieldName(String field) {
|
||||
//处理字段名忽视部分
|
||||
String[] ignore = Contants.FILED_IGNORE;
|
||||
for (int i = 0; i < ignore.length; i++) {
|
||||
if (field.contains(ignore[i])) {
|
||||
field = field.replace(ignore[i], "");
|
||||
}
|
||||
}
|
||||
//处理字段名
|
||||
String result = "";
|
||||
if (field.contains("_")) {
|
||||
String lowerFeild = field.toLowerCase();
|
||||
String[] fields = lowerFeild.split("_");
|
||||
result += fields[0];
|
||||
if (fields.length > 1) {
|
||||
for (int i = 1; i < fields.length; i++) {
|
||||
result += fields[i].substring(0, 1).toUpperCase() + fields[i].substring(1, fields[i].length());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result = field;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据表名获取类名
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return
|
||||
*/
|
||||
public static String getClassName(String tableName) {
|
||||
String result = "";
|
||||
String lowerFeild = tableName.toLowerCase();
|
||||
//处理表名
|
||||
String[] ignore = Contants.TABLE_IGNORE;
|
||||
for (int i = 0; i < ignore.length; i++) {
|
||||
if (lowerFeild.contains(ignore[i])) {
|
||||
lowerFeild = lowerFeild.replace(ignore[i], "");
|
||||
}
|
||||
}
|
||||
String[] fields = lowerFeild.split("_");
|
||||
if (fields.length >= 1) {
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
result += fields[i].substring(0, 1).toUpperCase() + fields[i].substring(1, fields[i].length());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* 取得已经构造生成的数据库连接
|
||||
*
|
||||
* @return 返回数据库连接对象
|
||||
* @throws Exception
|
||||
*/
|
||||
public static Connection getConnect(DataSource data) throws Exception {
|
||||
Connection con = null;
|
||||
try {
|
||||
GetDbConnect(data);
|
||||
con = dataSource.getConnection();
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
}
|
||||
return con;
|
||||
}
|
||||
}
|
||||
41
op-tools/src/main/java/cn/feast/coding/tools/Contants.java
Normal file
41
op-tools/src/main/java/cn/feast/coding/tools/Contants.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
public class Contants {
|
||||
public static final String SSM_FRAME = "ssm";
|
||||
public static final String SSH_FRAME = "ssh";
|
||||
public static final String JSP_FRAME = "jsp";
|
||||
public static final String SSM_CUSTOM_FRAME = "ssm-custom";
|
||||
public static final String SSM_MAVEN_FRAME = "ssm-maven";
|
||||
//servlet模板路径
|
||||
public static final String SERVLET_TEMPLATE_PATH = "/template/servlet";
|
||||
//ssm模板路径
|
||||
public static final String SSM_TEMPLATE_PATH = "/template/ssm";
|
||||
//ssh模板路径
|
||||
public static final String SSH_TEMPLATE_PATH = "/template/ssh";
|
||||
//ssm-custom模板路径
|
||||
public static final String SSM_CUSTOM_TEMPLATE_PATH = "/template/ssm-custom";
|
||||
//ssm-maven模板路径
|
||||
public static final String SSM_MAVEN_TEMPLATE_PATH = "/template/ssm-maven";
|
||||
//表名过滤关键字
|
||||
public static final String[] TABLE_IGNORE = new String[]{"sys_","gha_","tb_","db_", "t_", "nba_"};
|
||||
//字段名过滤关键字
|
||||
public static final String[] FILED_IGNORE = new String[]{};
|
||||
//空格
|
||||
public static final String BLACK_SPACE = " ";
|
||||
//String类型
|
||||
public static final String STRING_TYPE = "String";
|
||||
//int类型
|
||||
public static final String INT_TYPE = "int";
|
||||
//colunm显示
|
||||
public static final Integer COLUNM_IS_SHOWED = 0;
|
||||
//colunm不显示
|
||||
public static final Integer COLUNM_NOT_SHOWED = 1;
|
||||
//login模块
|
||||
public static final String LOGIN_MODULE = "login";
|
||||
//register模块
|
||||
public static final String REGISTER_MODULE = "register";
|
||||
//query模块
|
||||
public static final String QUERY_MODULE = "query";
|
||||
//password模块
|
||||
public static final String PASSWORD_MODULE = "password";
|
||||
}
|
||||
241
op-tools/src/main/java/cn/feast/coding/tools/DataBaseTools.java
Normal file
241
op-tools/src/main/java/cn/feast/coding/tools/DataBaseTools.java
Normal file
@@ -0,0 +1,241 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
import cn.feast.coding.model.generator.Colunm;
|
||||
import cn.feast.coding.model.generator.DataSource;
|
||||
import cn.feast.coding.model.generator.Table;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DataBaseTools {
|
||||
/**
|
||||
* 获取数据库所有数据表名称
|
||||
*
|
||||
* @param dataSource
|
||||
* @return
|
||||
*/
|
||||
public static List<Table> getTables(DataSource dataSource) {
|
||||
List<Table> tables = new ArrayList<Table>();
|
||||
Table table = null;
|
||||
Connection conn = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
conn = getConn(dataSource);
|
||||
DatabaseMetaData dbMetaData = conn.getMetaData();
|
||||
rs = dbMetaData.getTables(null, null, null, new String[]{"TABLE"});
|
||||
while (rs.next()) {
|
||||
table = new Table();
|
||||
String tableName = rs.getString("TABLE_NAME");
|
||||
table.setTableName(tableName);
|
||||
table.setClassName(getClassName(tableName));
|
||||
tables.add(table);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
closeConn(rs, null, conn);
|
||||
}
|
||||
return tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取表数据
|
||||
*
|
||||
* @param dataSource 数据源
|
||||
* @param tableName 表名
|
||||
* @return
|
||||
*/
|
||||
public static List<Colunm> readData(DataSource dataSource,
|
||||
String tableName) {
|
||||
List<Colunm> properties = new ArrayList<Colunm>();
|
||||
Colunm colunm = null;
|
||||
Connection conn = null;
|
||||
ResultSet rs = null;
|
||||
String primaryKey = genFieldName(getPrimaryKey(dataSource, tableName));
|
||||
try {
|
||||
conn = getConn(dataSource);
|
||||
DatabaseMetaData dbmd = conn.getMetaData();
|
||||
rs = dbmd.getColumns(null, null, tableName, null);
|
||||
while (rs.next()) {
|
||||
colunm = new Colunm();
|
||||
String colunmName = genFieldName(rs.getString("COLUMN_NAME"));
|
||||
colunm.setColunmName(colunmName);
|
||||
colunm.setFiledName(rs.getString("COLUMN_NAME"));
|
||||
if (colunmName.equals(primaryKey)) {
|
||||
colunm.setIsPrimary(1);
|
||||
} else {
|
||||
colunm.setIsPrimary(0);
|
||||
}
|
||||
int dataType = rs.getInt("DATA_TYPE"); //对应的java.sql.Types类型
|
||||
int columnSize = rs.getInt("COLUMN_SIZE");//列大小
|
||||
int decimalDigits = rs.getInt("DECIMAL_DIGITS");//小数位数
|
||||
colunm.setColunmType(JavaType.getType(dataType, columnSize, decimalDigits));
|
||||
colunm.setShowName(rs.getString("REMARKS"));
|
||||
properties.add(colunm);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
closeConn(rs, null, conn);
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取数据表主键
|
||||
*
|
||||
* @param dataSource
|
||||
* @param tableName
|
||||
* @return
|
||||
*/
|
||||
public static String getPrimaryKey(DataSource dataSource, String tableName) {
|
||||
Connection conn = null;
|
||||
ResultSet rs = null;
|
||||
String primaryKeyName = "";
|
||||
try {
|
||||
conn = getConn(dataSource);
|
||||
rs = conn.getMetaData().getPrimaryKeys(null, null, tableName);
|
||||
if (rs.next()) {
|
||||
primaryKeyName = rs.getString(4);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
closeConn(rs, null, conn);
|
||||
}
|
||||
|
||||
if (StringUtils.isNotEmpty(primaryKeyName)) {
|
||||
return primaryKeyName;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据表字段名获取java中的字段名
|
||||
*
|
||||
* @param field 字段名
|
||||
* @return
|
||||
*/
|
||||
public static String genFieldName(String field) {
|
||||
if(StringUtils.isEmpty(field)){
|
||||
return "";
|
||||
}
|
||||
//处理字段名忽视部分
|
||||
String[] ignore = Contants.FILED_IGNORE;
|
||||
for (int i = 0; i < ignore.length; i++) {
|
||||
if (field.contains(ignore[i])) {
|
||||
field = field.replace(ignore[i], "");
|
||||
}
|
||||
}
|
||||
//处理字段名
|
||||
String result = "";
|
||||
if (field.contains("_")) {
|
||||
String lowerFeild = field.toLowerCase();
|
||||
String[] fields = lowerFeild.split("_");
|
||||
result += fields[0];
|
||||
if (fields.length > 1) {
|
||||
for (int i = 1; i < fields.length; i++) {
|
||||
result += fields[i].substring(0, 1).toUpperCase() + fields[i].substring(1, fields[i].length());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result = field;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据表名获取类名
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return
|
||||
*/
|
||||
public static String getClassName(String tableName) {
|
||||
String result = "";
|
||||
String lowerFeild = tableName.toLowerCase();
|
||||
//处理表名
|
||||
String[] ignore = Contants.TABLE_IGNORE;
|
||||
for (int i = 0; i < ignore.length; i++) {
|
||||
if (lowerFeild.contains(ignore[i])) {
|
||||
lowerFeild = lowerFeild.replace(ignore[i], "");
|
||||
}
|
||||
}
|
||||
String[] fields = lowerFeild.split("_");
|
||||
if (fields.length >= 1) {
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
result += fields[i].substring(0, 1).toUpperCase() + fields[i].substring(1, fields[i].length());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据库连接
|
||||
*
|
||||
* @param dataSource
|
||||
* @return
|
||||
*/
|
||||
public static Connection getConn(DataSource dataSource) {
|
||||
Connection conn = null;
|
||||
try {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
conn = DriverManager.getConnection(dataSource.getJdbcUrl(), dataSource.getJdbcUser(), dataSource.getJdbcPass());
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return conn;
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭数据库连接
|
||||
*
|
||||
* @param rs
|
||||
* @param stm
|
||||
* @param conn
|
||||
*/
|
||||
public static void closeConn(ResultSet rs, Statement stm, Connection conn) {
|
||||
try {
|
||||
if (rs != null)
|
||||
rs.close();
|
||||
if (stm != null)
|
||||
stm.close();
|
||||
if (conn != null)
|
||||
conn.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试数据库连接
|
||||
*
|
||||
* @param dataSource
|
||||
* @return
|
||||
*/
|
||||
public static int testConnection(DataSource dataSource) {
|
||||
Connection conn = null;
|
||||
ResultSet rs = null;
|
||||
Statement st = null;
|
||||
int data = 0;
|
||||
String sql = "select 1";
|
||||
try {
|
||||
conn = getConn(dataSource);
|
||||
st = conn.createStatement();
|
||||
rs = st.executeQuery(sql);
|
||||
if (rs.next()) {
|
||||
data = rs.getInt(1);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return data;
|
||||
} finally {
|
||||
closeConn(rs, st, conn);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
14
op-tools/src/main/java/cn/feast/coding/tools/DateUtils.java
Normal file
14
op-tools/src/main/java/cn/feast/coding/tools/DateUtils.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class DateUtils {
|
||||
private final static String YYYY_MM_DD_FORMATTER = "yyyy-MM-dd";
|
||||
|
||||
|
||||
public static String getDate(Date date) {
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_FORMATTER);
|
||||
return simpleDateFormat.format(date);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class FileDeleteUtils {
|
||||
/**
|
||||
* 删除空目录
|
||||
* @param dir 将要删除的目录路径
|
||||
*/
|
||||
public static void doDeleteEmptyDir(String dir) {
|
||||
boolean success = (new File(dir)).delete();
|
||||
if (success) {
|
||||
System.out.println("Successfully deleted empty directory: " + dir);
|
||||
} else {
|
||||
System.out.println("Failed to delete empty directory: " + dir);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归删除目录下的所有文件及子目录下所有文件
|
||||
* @param dir 将要删除的文件目录
|
||||
* @return boolean Returns "true" if all deletions were successful.
|
||||
* If a deletion fails, the method stops attempting to
|
||||
* delete and returns "false".
|
||||
*/
|
||||
public static boolean deleteDir(File dir) {
|
||||
if (dir.isDirectory()) {
|
||||
String[] children = dir.list();
|
||||
//递归删除目录中的子目录下
|
||||
for (int i=0; i<children.length; i++) {
|
||||
boolean success = deleteDir(new File(dir, children[i]));
|
||||
if (!success) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 目录此时为空,可以删除
|
||||
return dir.delete();
|
||||
}
|
||||
}
|
||||
125
op-tools/src/main/java/cn/feast/coding/tools/HttpRequest.java
Normal file
125
op-tools/src/main/java/cn/feast/coding/tools/HttpRequest.java
Normal file
@@ -0,0 +1,125 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class HttpRequest {
|
||||
/**
|
||||
* 向指定URL发送GET方法的请求
|
||||
*
|
||||
* @param url
|
||||
* 发送请求的URL
|
||||
* @param param
|
||||
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
||||
* @return URL 所代表远程资源的响应结果
|
||||
*/
|
||||
public static String sendGet(String url, String param) {
|
||||
String result = "";
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
String urlNameString = url + "?" + param;
|
||||
URL realUrl = new URL(urlNameString);
|
||||
// 打开和URL之间的连接
|
||||
URLConnection connection = realUrl.openConnection();
|
||||
// 设置通用的请求属性
|
||||
connection.setRequestProperty("accept", "*/*");
|
||||
connection.setRequestProperty("connection", "Keep-Alive");
|
||||
connection.setRequestProperty("user-agent",
|
||||
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||
// 建立实际的连接
|
||||
connection.connect();
|
||||
// 获取所有响应头字段
|
||||
Map<String, List<String>> map = connection.getHeaderFields();
|
||||
// 遍历所有的响应头字段
|
||||
for (String key : map.keySet()) {
|
||||
System.out.println(key + "--->" + map.get(key));
|
||||
}
|
||||
// 定义 BufferedReader输入流来读取URL的响应
|
||||
in = new BufferedReader(new InputStreamReader(
|
||||
connection.getInputStream(), "gbk"));
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
result += line;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("发送GET请求出现异常!" + e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 使用finally块来关闭输入流
|
||||
finally {
|
||||
try {
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
} catch (Exception e2) {
|
||||
e2.printStackTrace();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 向指定 URL 发送POST方法的请求
|
||||
*
|
||||
* @param url
|
||||
* 发送请求的 URL
|
||||
* @param param
|
||||
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
||||
* @return 所代表远程资源的响应结果
|
||||
*/
|
||||
public static String sendPost(String url, String param) {
|
||||
PrintWriter out = null;
|
||||
BufferedReader in = null;
|
||||
String result = "";
|
||||
try {
|
||||
URL realUrl = new URL(url);
|
||||
// 打开和URL之间的连接
|
||||
URLConnection conn = realUrl.openConnection();
|
||||
// 设置通用的请求属性
|
||||
conn.setRequestProperty("accept", "*/*");
|
||||
conn.setRequestProperty("connection", "Keep-Alive");
|
||||
conn.setRequestProperty("user-agent",
|
||||
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||
// 发送POST请求必须设置如下两行
|
||||
conn.setDoOutput(true);
|
||||
conn.setDoInput(true);
|
||||
// 获取URLConnection对象对应的输出流
|
||||
out = new PrintWriter(conn.getOutputStream());
|
||||
// 发送请求参数
|
||||
out.print(param);
|
||||
// flush输出流的缓冲
|
||||
out.flush();
|
||||
// 定义BufferedReader输入流来读取URL的响应
|
||||
in = new BufferedReader(
|
||||
new InputStreamReader(conn.getInputStream()));
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
result += line;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("发送 POST 请求出现异常!"+e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
//使用finally块来关闭输出流、输入流
|
||||
finally{
|
||||
try{
|
||||
if(out!=null){
|
||||
out.close();
|
||||
}
|
||||
if(in!=null){
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
catch(IOException ex){
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
public class JSPPageTemplate {
|
||||
/**
|
||||
* model模板
|
||||
*/
|
||||
public static final String MODEL_TEMPLATE = "JSPModelTemplate.ftl";
|
||||
/**
|
||||
* dao模板
|
||||
*/
|
||||
public static final String DAO_TEMPLATE = "JSPDaoTemplate.ftl";
|
||||
/**
|
||||
* DBUtils模板
|
||||
*/
|
||||
public static final String DBUTILS_TEMPLATE = "JSPDBUtilsTemplate.ftl";
|
||||
/**
|
||||
* PageUtils模板
|
||||
*/
|
||||
public static final String PAGE_UTILS_TEMPLATE = "JSPPageUtilsTemplate.ftl";
|
||||
/**
|
||||
* Page模板
|
||||
*/
|
||||
public static final String PAGE_TEMPLATE = "JSPPageTemplate.ftl";
|
||||
/**
|
||||
* xml模板
|
||||
*/
|
||||
public static final String XML_TEMPLATE = "JSPXmlTemplate.ftl";
|
||||
/**
|
||||
* servlet模板
|
||||
*/
|
||||
public static final String QUERY_SERVLET_TEMPLATE = "JSPQueryServletTemplate.ftl";
|
||||
public static final String ADD_SERVLET_TEMPLATE = "JSPAddServletTemplate.ftl";
|
||||
public static final String EDIT_SERVLET_TEMPLATE = "JSPEditServletTemplate.ftl";
|
||||
public static final String UPDATE_SERVLET_TEMPLATE = "JSPUpdateServletTemplate.ftl";
|
||||
public static final String DELETE_SERVLET_TEMPLATE = "JSPDeleteServletTemplate.ftl";
|
||||
public static final String LOGIN_SERVLET_TEMPLATE = "JSPLoginServletTemplate.ftl";
|
||||
/**
|
||||
* 查询servlet模板
|
||||
*/
|
||||
public static final String QUERY_SERVLET_SELECTED_TEMPLATE = "JSPQuerySelectedServletTemplate.ftl";
|
||||
/**
|
||||
* 自定义模板
|
||||
*/
|
||||
public static final String DEFINED_TEMPLATE = "JSPDefinedTemplate.ftl";
|
||||
}
|
||||
98
op-tools/src/main/java/cn/feast/coding/tools/JavaType.java
Normal file
98
op-tools/src/main/java/cn/feast/coding/tools/JavaType.java
Normal file
@@ -0,0 +1,98 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
import java.sql.Types;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author feastcoding
|
||||
*/
|
||||
public class JavaType {
|
||||
public final static String UNKNOW = "UNKNOW";
|
||||
public final static String SPECIAL = "SPECIAL";
|
||||
|
||||
public static Map<Integer, String> mapping = new HashMap<Integer, String>();
|
||||
|
||||
static {
|
||||
mapping.put(Types.BIGINT, "Long");
|
||||
mapping.put(Types.BINARY, "byte[]");
|
||||
mapping.put(Types.BIT, "Integer");
|
||||
mapping.put(Types.BLOB, "byte[]");
|
||||
mapping.put(Types.BOOLEAN, "Integer");
|
||||
mapping.put(Types.CHAR, "String");
|
||||
mapping.put(Types.CLOB, "String");
|
||||
mapping.put(Types.DATALINK, UNKNOW);
|
||||
mapping.put(Types.DATE, "Date");
|
||||
mapping.put(Types.DECIMAL, "Double");
|
||||
mapping.put(Types.DISTINCT, UNKNOW);
|
||||
mapping.put(Types.DOUBLE, "Double");
|
||||
mapping.put(Types.FLOAT, "Float");
|
||||
mapping.put(Types.INTEGER, "Integer");
|
||||
mapping.put(Types.JAVA_OBJECT, UNKNOW);
|
||||
// mapping.put(Types.LONGNVARCHAR, "String");
|
||||
mapping.put(Types.LONGVARBINARY, "byte[]");
|
||||
mapping.put(Types.LONGVARCHAR, "String");
|
||||
// mapping.put(Types.NCHAR, "String");
|
||||
// mapping.put(Types.NCLOB, "String");
|
||||
mapping.put(Types.NULL, UNKNOW);
|
||||
mapping.put(Types.NUMERIC, SPECIAL);
|
||||
mapping.put(Types.OTHER, "Object");
|
||||
mapping.put(Types.REAL, "Double");
|
||||
mapping.put(Types.REF, UNKNOW);
|
||||
// mapping.put(Types.REF_CURSOR, UNKNOW);
|
||||
mapping.put(Types.SMALLINT, "Integer");
|
||||
// mapping.put(Types.SQLXML, "String");
|
||||
mapping.put(Types.STRUCT, UNKNOW);
|
||||
mapping.put(Types.TIME, "Date");
|
||||
// mapping.put(Types.TIME_WITH_TIMEZONE, "Timestamp");
|
||||
mapping.put(Types.TIMESTAMP, "Date");
|
||||
// mapping.put(Types.TIMESTAMP_WITH_TIMEZONE, "Timestamp");
|
||||
mapping.put(Types.TINYINT, "Integer");
|
||||
mapping.put(Types.VARBINARY, "byte[]");
|
||||
mapping.put(Types.VARCHAR, "String");
|
||||
|
||||
}
|
||||
|
||||
public static boolean isDateType(Integer sqlType) {
|
||||
// if (sqlType == Types.DATE || sqlType == Types.TIME || sqlType == Types.TIME_WITH_TIMEZONE || sqlType == Types.TIMESTAMP || sqlType == Types.TIMESTAMP_WITH_TIMEZONE) {
|
||||
// return true;
|
||||
// } else {
|
||||
// return false;
|
||||
// }
|
||||
if (sqlType == Types.DATE || sqlType == Types.TIME || sqlType == Types.TIMESTAMP) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isInteger(Integer sqlType) {
|
||||
if (sqlType == Types.BOOLEAN || sqlType == Types.BIT || sqlType == Types.INTEGER || sqlType == Types.TINYINT || sqlType == Types.SMALLINT) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getType(Integer sqlType, Integer size, Integer digit) {
|
||||
String type = mapping.get(sqlType);
|
||||
if (type.equals(SPECIAL)) {
|
||||
|
||||
if (digit != null) {
|
||||
return "Double";
|
||||
} else {
|
||||
if (size >= 12) {
|
||||
return "Long";
|
||||
} else {
|
||||
return "Integer";
|
||||
}
|
||||
}
|
||||
} else if (type.equals(Types.TINYINT)) {
|
||||
return "Boolean";
|
||||
} else {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
70
op-tools/src/main/java/cn/feast/coding/tools/JsonResult.java
Normal file
70
op-tools/src/main/java/cn/feast/coding/tools/JsonResult.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class JsonResult implements Serializable {
|
||||
public static final int SUCCESS = 0;
|
||||
public static final int ERROR = 1;
|
||||
|
||||
private int state;
|
||||
/**
|
||||
* 错误消息
|
||||
*/
|
||||
private String message;
|
||||
/**
|
||||
* 返回正确时候的数据
|
||||
*/
|
||||
private Object data;
|
||||
|
||||
public JsonResult() {
|
||||
}
|
||||
|
||||
public JsonResult(String error) {
|
||||
state = ERROR;
|
||||
this.message = error;
|
||||
}
|
||||
|
||||
public JsonResult(Object data) {
|
||||
state = SUCCESS;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public JsonResult(Throwable e) {
|
||||
state = ERROR;
|
||||
message = e.getMessage();
|
||||
}
|
||||
|
||||
public JsonResult(int state, Throwable e) {
|
||||
this.state = state;
|
||||
this.message = e.getMessage();
|
||||
}
|
||||
|
||||
public int getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(int state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(Object data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "JsonResult [state=" + state + ", message=" + message + ", data=" + data + "]";
|
||||
}
|
||||
}
|
||||
24
op-tools/src/main/java/cn/feast/coding/tools/ListUtils.java
Normal file
24
op-tools/src/main/java/cn/feast/coding/tools/ListUtils.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by IntelliJ IDEA.
|
||||
* Creator : peng
|
||||
* Date : 2018-03-20
|
||||
* Time : 15:41
|
||||
*/
|
||||
public class ListUtils {
|
||||
public static List getListByNum(List l, Integer num) {
|
||||
List list = new ArrayList();
|
||||
if (l.size() < num) {
|
||||
list = l;
|
||||
} else {
|
||||
for (int i = 0; i < num; i++) {
|
||||
list.add(l.get(i));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
public class PageTemplateUtils {
|
||||
public static final String ADD_PAGE = "add.ftl";
|
||||
public static final String EDIT_PAGE = "edit.ftl";
|
||||
public static final String LIST_PAGE = "list.ftl";
|
||||
public static final String DETAIL_PAGE = "detail.ftl";
|
||||
public static final String INDEX_PAGE = "index.ftl";
|
||||
public static final String LOGIN_PAGE = "login.ftl";
|
||||
public static final String REGISTER_PAGE = "register.ftl";
|
||||
public static final String PASSWORD_PAGE = "password.ftl";
|
||||
public static final String WELCOME_PAGE = "welcome.ftl";
|
||||
}
|
||||
37
op-tools/src/main/java/cn/feast/coding/tools/PageUtils.java
Normal file
37
op-tools/src/main/java/cn/feast/coding/tools/PageUtils.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
public class PageUtils
|
||||
{
|
||||
public static int pageSize(HttpServletRequest request, Integer pageSize)
|
||||
{
|
||||
int size;
|
||||
if (pageSize == null)
|
||||
size = SysContants.MAX_PAGE_ROWS;
|
||||
else
|
||||
size = pageSize;
|
||||
request.getSession().setAttribute("pageSize", size);
|
||||
return size;
|
||||
}
|
||||
|
||||
public static int getPageNum(Integer pageNum)
|
||||
{
|
||||
return (pageNum == null || pageNum < 1) ? 1 : pageNum;
|
||||
}
|
||||
|
||||
public static int getPage(int pageNumber,int totalPage,int totalNumber,int maxRows){
|
||||
totalNumber = totalNumber/maxRows+(totalNumber%maxRows==0?0:1);
|
||||
totalPage = totalNumber;
|
||||
if(pageNumber>totalPage)pageNumber=totalNumber;
|
||||
if(totalPage == 0){
|
||||
return 0;
|
||||
}else{
|
||||
return (pageNumber-1)*maxRows;
|
||||
}
|
||||
}
|
||||
|
||||
public static int getTotalPage(int pageNumber,int totalPage,int totalNumber,int maxRows){
|
||||
return totalNumber/maxRows+(totalNumber%maxRows==0?0:1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
public class SSHPageTemplate {
|
||||
/**
|
||||
* model模板
|
||||
*/
|
||||
public static final String MODEL_TEMPLATE = "SSHBeanTemplate.ftl";
|
||||
/**
|
||||
* dao模板
|
||||
*/
|
||||
public static final String MAPPER_TEMPLATE = "SSHDaoTemplate.ftl";
|
||||
/**
|
||||
* daoImpl模板
|
||||
*/
|
||||
public static final String MAPPER_XML_TEMPLATE = "SSHDaoImplTemplate.ftl";
|
||||
/**
|
||||
* service模板
|
||||
*/
|
||||
public static final String SERVICE_TEMPLATE = "SSHServiceTemplate.ftl";
|
||||
/**
|
||||
* controller模板
|
||||
*/
|
||||
public static final String CONTROLLER_TEMPLATE = "SSHActionTemplate.ftl";
|
||||
/**
|
||||
* pageUtils模板
|
||||
*/
|
||||
public static final String PAGE_UTILS_TEMPLATE = "SSHPageUtils.ftl";
|
||||
/**
|
||||
* Page模板
|
||||
*/
|
||||
public static final String PAGE_TEMPLATE = "SSHPageTemplate.ftl";
|
||||
/**
|
||||
* Struts模板
|
||||
*/
|
||||
public static final String STRUTS_TEMPLATE = "SSHStruts.ftl";
|
||||
/**
|
||||
* 自定义模板
|
||||
*/
|
||||
public static final String DEFINED_TEMPLATE = "SSHDefinedTemplate.ftl";
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
public class SSMCustomPageTemplate {
|
||||
/**
|
||||
* model模板
|
||||
*/
|
||||
public static final String MODEL_TEMPLATE = "ModelTemplate.ftl";
|
||||
/**
|
||||
* mapper模板
|
||||
*/
|
||||
public static final String MAPPER_TEMPLATE = "MapperTemplate.ftl";
|
||||
/**
|
||||
* mapperXml模板
|
||||
*/
|
||||
public static final String MAPPER_XML_TEMPLATE = "MapperXmlTemplate.ftl";
|
||||
/**
|
||||
* service模板
|
||||
*/
|
||||
public static final String SERVICE_TEMPLATE = "ServiceTemplate.ftl";
|
||||
/**
|
||||
* service模板
|
||||
*/
|
||||
public static final String SERVICE_IMPL_TEMPLATE = "ServiceImplTemplate.ftl";
|
||||
/**
|
||||
* controller模板
|
||||
*/
|
||||
public static final String CONTROLLER_TEMPLATE = "ControllerTemplate.ftl";
|
||||
/**
|
||||
* IMapper模板
|
||||
*/
|
||||
public static final String IMAPPER_TEMPLATE = "IMapperTemplate.ftl";
|
||||
/**
|
||||
* IService模板
|
||||
*/
|
||||
public static final String ISERVICE_TEMPLATE = "IServiceTemplate.ftl";
|
||||
/**
|
||||
* IService模板
|
||||
*/
|
||||
public static final String PAGEUTILS_TEMPLATE = "PageUtilsTemplate.ftl";
|
||||
/**
|
||||
* IService模板
|
||||
*/
|
||||
public static final String BASE_CONTROLLER_TEMPLATE = "BaseControllerTemplate.ftl";
|
||||
/**
|
||||
* 自定义模板
|
||||
*/
|
||||
public static final String DEFINED_TEMPLATE = "DefinedTemplate.ftl";
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
public class SSMMavenPageTemplate {
|
||||
/**
|
||||
* model模板
|
||||
*/
|
||||
public static final String MODEL_TEMPLATE = "ModelTemplate.ftl";
|
||||
/**
|
||||
* mapper模板
|
||||
*/
|
||||
public static final String MAPPER_TEMPLATE = "MapperTemplate.ftl";
|
||||
/**
|
||||
* mapperXml模板
|
||||
*/
|
||||
public static final String MAPPER_XML_TEMPLATE = "MapperXmlTemplate.ftl";
|
||||
/**
|
||||
* service模板
|
||||
*/
|
||||
public static final String SERVICE_TEMPLATE = "ServiceTemplate.ftl";
|
||||
/**
|
||||
* service模板
|
||||
*/
|
||||
public static final String SERVICE_IMPL_TEMPLATE = "ServiceImplTemplate.ftl";
|
||||
/**
|
||||
* controller模板
|
||||
*/
|
||||
public static final String CONTROLLER_TEMPLATE = "ControllerTemplate.ftl";
|
||||
/**
|
||||
* IMapper模板
|
||||
*/
|
||||
public static final String IMAPPER_TEMPLATE = "IMapperTemplate.ftl";
|
||||
/**
|
||||
* IService模板
|
||||
*/
|
||||
public static final String ISERVICE_TEMPLATE = "IServiceTemplate.ftl";
|
||||
/**
|
||||
* IService模板
|
||||
*/
|
||||
public static final String PAGEUTILS_TEMPLATE = "PageUtilsTemplate.ftl";
|
||||
/**
|
||||
* IService模板
|
||||
*/
|
||||
public static final String BASE_CONTROLLER_TEMPLATE = "BaseControllerTemplate.ftl";
|
||||
/**
|
||||
* pom.xml模板
|
||||
*/
|
||||
public static final String POM_XML_TEMPLATE = "pom.ftl";
|
||||
/**
|
||||
* web.xml模板
|
||||
*/
|
||||
public static final String WEB_XML_TEMPLATE = "webXml.ftl";
|
||||
/**
|
||||
* spring-core.xml模板
|
||||
*/
|
||||
public static final String SPRING_CORE_XML_TEMPLATE = "springCore.ftl";
|
||||
/**
|
||||
* spring-web.xml模板
|
||||
*/
|
||||
public static final String SPRING_WEB_XML_TEMPLATE = "springWeb.ftl";
|
||||
/**
|
||||
* logback.xml模板
|
||||
*/
|
||||
public static final String LOGBACK_XML_TEMPLATE = "logback.ftl";
|
||||
/**
|
||||
* mybatis-config.xml模板
|
||||
*/
|
||||
public static final String MYBATIS_CONFIG_XML_TEMPLATE = "mybatisConfig.ftl";
|
||||
/**
|
||||
* jdbc.properties模板
|
||||
*/
|
||||
public static final String JDBC_PROPERTIES_TEMPLATE = "jdbc.ftl";
|
||||
/**
|
||||
* 自定义模板
|
||||
*/
|
||||
public static final String DEFINED_TEMPLATE = "DefinedTemplate.ftl";
|
||||
/**
|
||||
* 登录拦截器模板
|
||||
*/
|
||||
public static final String LOGIN_INTERCEPTOR_TEMPLATE = "LoginInterceptor.ftl";
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
public class SSMPageTemplate {
|
||||
/**
|
||||
* model模板
|
||||
*/
|
||||
public static final String MODEL_TEMPLATE = "SSMModelTemplate.ftl";
|
||||
/**
|
||||
* mapper模板
|
||||
*/
|
||||
public static final String MAPPER_TEMPLATE = "SSMMapperTemplate.ftl";
|
||||
/**
|
||||
* mapperXml模板
|
||||
*/
|
||||
public static final String MAPPER_XML_TEMPLATE = "SSMMapperXmlTemplate.ftl";
|
||||
/**
|
||||
* service模板
|
||||
*/
|
||||
public static final String SERVICE_TEMPLATE = "SSMServiceTemplate.ftl";
|
||||
/**
|
||||
* controller模板
|
||||
*/
|
||||
public static final String CONTROLLER_TEMPLATE = "SSMControllerTemplate.ftl";
|
||||
/**
|
||||
* page模板
|
||||
*/
|
||||
public static final String PAGE_TEMPLATE = "SSMPageTemplate.ftl";
|
||||
/**
|
||||
* pageUtils模板
|
||||
*/
|
||||
public static final String PAGE_UTILS_TEMPLATE = "SSMPageUtilsTemplate.ftl";
|
||||
/**
|
||||
* 自定义模板
|
||||
*/
|
||||
public static final String DEFINED_TEMPLATE = "SSMDefinedTemplate.ftl";
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
public class StringEncodeUtils {
|
||||
|
||||
public static String utf8ToIso88591(String str) {
|
||||
String ret = "";
|
||||
try {
|
||||
ret = new String(str.getBytes("UTF-8"), "ISO-8859-1");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static String iso88591ToUtf8(String str) {
|
||||
String ret = "";
|
||||
try {
|
||||
ret = new String(str.getBytes("ISO-8859-1"), "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static String gbkToIso88591(String str) {
|
||||
String ret = "";
|
||||
try {
|
||||
ret = new String(str.getBytes("GBK"), "ISO-8859-1");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static String convertEncode(String str, String source, String replacement) {
|
||||
String ret = "";
|
||||
try {
|
||||
ret = new String(str.getBytes(source), replacement);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
110
op-tools/src/main/java/cn/feast/coding/tools/StringUtils.java
Normal file
110
op-tools/src/main/java/cn/feast/coding/tools/StringUtils.java
Normal file
@@ -0,0 +1,110 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class StringUtils {
|
||||
/**
|
||||
* 返回一个字符串首字母大写
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static String capFirst(String str) {
|
||||
str = str.substring(0, 1).toUpperCase() + str.substring(1);
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回一个字符串首字母小写
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static String uncapFirst(String str) {
|
||||
str = str.substring(0, 1).toLowerCase() + str.substring(1);
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换字符串中占位符(${})的内容,模拟el表达式的字符串替换
|
||||
* @param openToken
|
||||
* @param closeToken
|
||||
* @param text
|
||||
* @param map
|
||||
* @return
|
||||
*/
|
||||
public static String parse(String openToken, String closeToken, String text, Map<String, Object> map) {
|
||||
if (map == null || map.size() <= 0) {
|
||||
return text;
|
||||
}
|
||||
if (text == null || text.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
char[] src = text.toCharArray();
|
||||
int offset = 0;
|
||||
// search open token
|
||||
int start = text.indexOf(openToken, offset);
|
||||
if (start == -1) {
|
||||
return text;
|
||||
}
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
StringBuilder expression = null;
|
||||
while (start > -1) {
|
||||
if (start > 0 && src[start - 1] == '\\') {
|
||||
// this open token is escaped. remove the backslash and continue.
|
||||
builder.append(src, offset, start - offset - 1).append(openToken);
|
||||
offset = start + openToken.length();
|
||||
} else {
|
||||
// found open token. let's search close token.
|
||||
if (expression == null) {
|
||||
expression = new StringBuilder();
|
||||
} else {
|
||||
expression.setLength(0);
|
||||
}
|
||||
builder.append(src, offset, start - offset);
|
||||
offset = start + openToken.length();
|
||||
int end = text.indexOf(closeToken, offset);
|
||||
while (end > -1) {
|
||||
if (end > offset && src[end - 1] == '\\') {
|
||||
// this close token is escaped. remove the backslash and continue.
|
||||
expression.append(src, offset, end - offset - 1).append(closeToken);
|
||||
offset = end + closeToken.length();
|
||||
end = text.indexOf(closeToken, offset);
|
||||
} else {
|
||||
expression.append(src, offset, end - offset);
|
||||
// offset = end + closeToken.length()
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (end == -1) {
|
||||
// close token was not found.
|
||||
builder.append(src, start, src.length - start);
|
||||
offset = src.length;
|
||||
} else {
|
||||
String value = map.get(expression.toString()) == null ? "" : (String) map.get(expression.toString());
|
||||
builder.append(value);
|
||||
offset = end + closeToken.length();
|
||||
}
|
||||
}
|
||||
start = text.indexOf(openToken, offset);
|
||||
}
|
||||
if (offset < src.length) {
|
||||
builder.append(src, offset, src.length - offset);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static String replacePlaceholder(String text, Map<String, Object> map){
|
||||
return parse("${", "}", text, map);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String text = "hello,${name},你好${pass},${name}";
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("name", "张三");
|
||||
map.put("pass", "123");
|
||||
String str = replacePlaceholder(text, map);
|
||||
System.out.println(str);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
public class SysContants {
|
||||
public static final String CURRENT_USER = "user";
|
||||
|
||||
public static final int MAX_PAGE_ROWS = 100;
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
public class UploadUtils {
|
||||
public static String upload(HttpServletRequest request,MultipartFile file,String picPath) throws IllegalStateException, IOException{
|
||||
String path = request.getSession().getServletContext().getRealPath(picPath);
|
||||
File f = new File(path);
|
||||
if(!f.exists()){
|
||||
f.mkdirs();
|
||||
}
|
||||
if(!file.isEmpty()){
|
||||
String saveName = UUID.randomUUID().toString() + file.getOriginalFilename().substring
|
||||
(file.getOriginalFilename().lastIndexOf("."));
|
||||
file.transferTo(new File(path + "/" + saveName));
|
||||
return saveName;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void deleteFile(String filePath,HttpServletRequest request){
|
||||
try {
|
||||
File file = new File(request.getSession().getServletContext().getRealPath("/"+filePath));
|
||||
if(file.exists()){
|
||||
file.delete();
|
||||
}else{
|
||||
throw new Exception("文件不存在:"+filePath);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载附件
|
||||
* @param fileName 附件名
|
||||
* @param fileDownloadPath 附件路径
|
||||
*/
|
||||
public static void download(String fileName,String fileDownloadPath,HttpServletResponse response){
|
||||
File file = new File(fileDownloadPath);
|
||||
download(file,fileName,response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载附件
|
||||
* @param file 附件
|
||||
* @param fileName 附件名
|
||||
*/
|
||||
public static void download(File file,String fileName,HttpServletResponse response){
|
||||
InputStream input = null;
|
||||
try {
|
||||
input = new FileInputStream(file);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
download(input, fileName, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载附件
|
||||
* @param input 附件流
|
||||
* @param fileName 附件名
|
||||
*/
|
||||
public static void download(InputStream input,String fileName,HttpServletResponse response){
|
||||
response.setCharacterEncoding("GBK");
|
||||
response.setHeader("Content-Type","application/octet-stream; charset=GBK");//
|
||||
response.setHeader("Content-Disposition","attachment; filename=" + StringEncodeUtils.gbkToIso88591(fileName));
|
||||
|
||||
OutputStream output = null;
|
||||
try {
|
||||
output = response.getOutputStream();
|
||||
IOUtils.copyLarge(input, output);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
IOUtils.closeQuietly(input);
|
||||
IOUtils.closeQuietly(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
95
op-tools/src/main/java/cn/feast/coding/tools/ZipTools.java
Normal file
95
op-tools/src/main/java/cn/feast/coding/tools/ZipTools.java
Normal file
@@ -0,0 +1,95 @@
|
||||
package cn.feast.coding.tools;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
public class ZipTools {
|
||||
/**
|
||||
* 压缩整个文件夹中的所有文件,生成指定名称的zip压缩包
|
||||
* @param filePath 文件所在目录
|
||||
* @param zipPath 压缩后zip文件名称
|
||||
* @param dirFlag zip文件中第一层是否包含一级目录,true包含;false没有
|
||||
* 2015年6月9日
|
||||
*/
|
||||
public static void zipMultiFile(String filePath ,String zipPath, boolean dirFlag) {
|
||||
File file = new File(filePath);// 要被压缩的文件夹
|
||||
File zipFile = new File(zipPath);
|
||||
try {
|
||||
if(!zipFile.getParentFile().exists()){
|
||||
zipFile.getParentFile().mkdir();
|
||||
}
|
||||
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
|
||||
if(file.isDirectory()){
|
||||
File[] files = file.listFiles();
|
||||
for(File fileSec:files){
|
||||
if(dirFlag){
|
||||
recursionZip(zipOut, fileSec, file.getName() + File.separator);
|
||||
}else{
|
||||
recursionZip(zipOut, fileSec, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
zipOut.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
FileDeleteUtils.deleteDir(file);
|
||||
}
|
||||
}
|
||||
|
||||
private static void recursionZip(ZipOutputStream zipOut, File file, String baseDir) throws Exception{
|
||||
if(file.isDirectory()){
|
||||
File[] files = file.listFiles();
|
||||
for(File fileSec:files){
|
||||
recursionZip(zipOut, fileSec, baseDir + file.getName() + File.separator);
|
||||
}
|
||||
}else{
|
||||
byte[] buf = new byte[1024];
|
||||
InputStream input = new FileInputStream(file);
|
||||
zipOut.putNextEntry(new ZipEntry(baseDir + file.getName()));
|
||||
int len;
|
||||
while((len = input.read(buf)) != -1){
|
||||
zipOut.write(buf, 0, len);
|
||||
}
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static HttpServletResponse downloadZip(File file, HttpServletResponse response) {
|
||||
if (file.exists() == false) {
|
||||
System.out.println("待压缩的文件目录:" + file + "不存在.");
|
||||
} else {
|
||||
try {
|
||||
// 以流的形式下载文件。
|
||||
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
|
||||
byte[] buffer = new byte[fis.available()];
|
||||
fis.read(buffer);
|
||||
fis.close();
|
||||
// 清空response
|
||||
response.reset();
|
||||
|
||||
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
|
||||
response.setContentType("application/octet-stream");
|
||||
//如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理
|
||||
response.setHeader("Content-Disposition", "attachment;filename="
|
||||
+ new String(file.getName().getBytes("GB2312"), "ISO8859-1"));
|
||||
toClient.write(buffer);
|
||||
toClient.flush();
|
||||
toClient.close();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
File f = new File(file.getPath());
|
||||
f.delete();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user