接口作用
定时任务生成的文件,通过ftp上传时,可以自己来通过这个接口定义上传过程。
接口内容
public interface FTPTransmitProcessor extends Level {
String MARK_STRING = "FTPTransmitProcessor";
int CURRENT_LEVEL = 1;
/**
* FTP上传的时候传输方式
* @param files 待上传的文件
*/
void transmit(File[] files, String serverAddress, int port, String username, String password, String savePath) throws Exception;
}
public class DefaultFTPTransmit extends AbstractFTPTransmitProcessor {
private static final int ARRAY_SIZE_FOR_FILE_BYTES = 1024;
public void transmit(File[] files, String serverAddress, int port, String username, String password, String savePath) throws Exception {
doFTP(files, serverAddress, port, username, password, savePath);
}
/**
* 将文件上传到FTP
*
* @param files 待上传的文件
* @throws Exception 异常
*/
private void doFTP(File[] files, String serverAddress, int port, String username, String password, String savePath) throws Exception {
Class ftpClientClass = FtpClient.class;
Object ftpClient;
Boolean newJDKVersionFlag = true;
// 根据客户端初始化方法判断 JDK 版本
try {
Method clientMethod = ftpClientClass.getMethod("create");
ftpClient = clientMethod.invoke(ftpClientClass);
} catch (NoSuchMethodException e) {
ftpClient = ftpClientClass.newInstance();
newJDKVersionFlag = false;
}
try {
Method changeDirectoryMethod = null;
Method binaryMethod = null;
Method fileStreamMethod = null;
if (newJDKVersionFlag) {
// JDK 1.7 版 FTP 上传功能相关方法
Method connectMethod = ftpClientClass.getMethod("connect", SocketAddress.class);
connectMethod.invoke(ftpClient, new InetSocketAddress(serverAddress, port));
Method loginMethod = ftpClientClass.getMethod("login", String.class, char[].class);
loginMethod.invoke(ftpClient, username, password.toCharArray());
changeDirectoryMethod = ftpClientClass.getMethod("changeDirectory", String.class);
binaryMethod = ftpClientClass.getMethod("setBinaryType");
fileStreamMethod = ftpClientClass.getMethod("putFileStream", String.class);
} else {
// JDK 1.6 及先前版本 FTP 上传功能相关方法
Method connectMethod = ftpClientClass.getMethod("openServer", String.class, int.class);
connectMethod.invoke(ftpClient, serverAddress, port);
Method loginMethod = ftpClientClass.getMethod("login", String.class, String.class);
loginMethod.invoke(ftpClient, username, password);
changeDirectoryMethod = ftpClientClass.getMethod("cd", String.class);
binaryMethod = ftpClientClass.getMethod("binary");
fileStreamMethod = ftpClientClass.getMethod("put", String.class);
}
if (StringUtils.isNotBlank(savePath)) {
changeDirectoryMethod.invoke(ftpClient, savePath);
}
binaryMethod.invoke(ftpClient);
outputFileToFTP(files, fileStreamMethod, ftpClient);
} catch (Exception e) {
ScheduleLogUtils.error(e);
throw new FTPException(e);
}
}
private void outputFileToFTP(File[] files, Method fileStreamMethod, Object ftpClient) throws Exception {
TelnetOutputStream os = null;
FileInputStream is = null;
try {
for (File file : files) {
if (file == null || !file.exists()) {
throw new IOException("File does not exit!");
}
os = (TelnetOutputStream) fileStreamMethod.invoke(ftpClient, file.getName());
is = new FileInputStream(file);
byte[] bytes = new byte[ARRAY_SIZE_FOR_FILE_BYTES];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
os.flush();
os.close();
}
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
}
}
注册方式
<extra-platform>
<extra-schedule>
<FTPTransmitProcessor class="com.fr.plugin.demo.ftp.YourFTPClassXXX"/>
</extra-schedule>
</extra-platform>