概述
通过properties配置两个FTP服务器,可以同步某个文件夹内容
实现FileAlterationListener监控文件夹文件新增/改变
通过后缀筛选文件类型
apache.commons包获取FTP连接客户端
net.sourceforge.javacsv包操作scv文件
pom文件
1、首先建议养成先配置日志的习惯
<!-- 日志工具包 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
<!--日志核心包-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
<!--slf4j的log4j实现类-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.9.1</version>
</dependency>
<!--程序运行的时候检测用了哪种日志实现类-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.9.1</version>
</dependency>
2、使用到的工具包
<!--功能强大,操作文件,FTP连接-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
<!--快速读取和生成CSV文件-->
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
public class ITransferImpl implements ITransfer {
@Override
public void transferInner(String fromAccount, String outAccount, BigDecimal amount) {
//使用java的JDBC
//1、注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
//Statement statement = null;
try {
//2、获取连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
//3、执行SQL对象
// statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
//4、查询转出账户余额是否大于转出金额
BigDecimal amount1 = null;
String fromAccountSQL = "SELECT amount from ACCOUNT where accountid = ?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(fromAccountSQL);
preparedStatement.setString(1,fromAccount);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
amount1 = resultSet.getBigDecimal("amount");
if (amount.compareTo(amount1) == 1){
System.out.println("账户金额不足!");
return;
}
}
//5、开启事务
connection.setAutoCommit(false);
//6、更新转出账户余额
BigDecimal updateFromAccount = amount1.subtract(amount);
String updateFromAccountSQL = "UPDATE ACCOUNT set amount = ? WHERE ACCOUNTID = ?";
PreparedStatement pps = connection.prepareStatement(updateFromAccountSQL);
pps.setBigDecimal(1,updateFromAccount);
pps.setString(2,fromAccount);
pps.executeUpdate();
//7、更新转入账户余额
//7.1先查询转入账户余额
PreparedStatement ppsOut = connection.prepareStatement(fromAccountSQL);
ppsOut.setString(1,outAccount);
ResultSet resultSet1 = ppsOut.executeQuery();
BigDecimal amount2 = null;
while (resultSet1.next()){
amount2 = resultSet1.getBigDecimal("amount");
}
//7.2余额与转入额相加
BigDecimal updateOutAccount = amount2.add(amount);
PreparedStatement ppsOutAccount = connection.prepareStatement(updateFromAccountSQL);
//ppsOutAccount.setBigDecimal(1,updateOutAccount);
ppsOutAccount.setString(2,outAccount);
ppsOutAccount.executeUpdate();
//8、提交事务
connection.commit();
//9、关闭连接
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
中秋快乐