(1)下面是发布模板的代码:
public void publishOneTemplate(){
try{
ssId = this.getFTPClientInfo();
if(ssId != null && !"".equals(ssId)){
this.connectServerAndSetFileType();
localFilePath = this.getFileAbsolutePath();
System.out.println("localFilePath="+localFilePath);
if(localFilePath != null && flag == true){
createDatabase(localFilePath);
this.createDirectoryName(publishWebsiteName,ftpClient,new File(localFilePath),localFilePath);
}
this.closeConnect();
}
}catch(java.net.SocketException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
(2)下面是自动建立数据库mysql
public void createDatabase(String path){
Connection connection = null;
try {
String jdbcFile = path + "/WEB-INF/conf/jdbc.properties";
InputStream in = new FileInputStream(jdbcFile);
Properties p = new Properties();
p.load(in);
String driver = p.getProperty("jdbc.driver");
String url = p.getProperty("jdbc.url");
String databaseUrl = url.substring(0, url.lastIndexOf("/"))+"/information_schema";
String databaseName = url.substring(url.lastIndexOf("/")+1,url.length());
String user = p.getProperty("jdbc.username");
String password = p.getProperty("jdbc.password");
Class.forName(driver);
connection = DriverManager.getConnection(databaseUrl, user, password);
connection.setAutoCommit(false);
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from SCHEMATA where SCHEMA_NAME='"+databaseName+"'");
if (!rs.next()){
statement.clearBatch();
statement.addBatch("create database if not exists `"+databaseName+"`");
File scriptFile = new File(path + "/WEB-INF/script/database.sql");
String script = FileUtil.convertFile2String(scriptFile);
statement.addBatch("USE `"+databaseName+"`");
String[] scripts = script.split(";");
for (int i = 0; i < scripts.length; i++){
if (!"".equals(scripts[i].trim()))
statement.addBatch(scripts[i]);
}
statement.executeBatch();
connection.commit();
}
} catch (FileNotFoundException e) {
logger.error(e.getMessage());
} catch (IOException e) {
logger.error(e.getMessage());
} catch (ClassNotFoundException e) {
logger.error(e.getMessage());
} catch (SQLException e) {
if (connection != null){
try {
connection.rollback();
} catch (SQLException e1) {
logger.error(e1.getMessage());
}
}
logger.error(e.getMessage());
} finally {
if (connection != null){
try {
connection.close();
} catch (SQLException e) {
logger.error(e.getMessage());
}
}
}
}
(3)这是它里面调用的一个方法。
public static String convertFile2String(File file, String encoding) throws IOException{
byte[] bs = new byte[new Long(file.length()).intValue()];
FileInputStream in = new FileInputStream(file);
try{
in.read(bs);
}finally{
in.close();
}
return encoding==null? new String(bs):new String(bs,encoding);
}
public static String convertFile2String(File file) throws IOException{
return convertFile2String(file,null);
}