我想要做的是重写onUploadStart
方法以进行自定义上传本地化(临时文件夹),然后重写onUploadEnd
方法以将成功上传的文件复制到其原始目标文件夹。Apache Mina FTP Server 主页说“将调用此通知方法以指示文件传输成功并且服务器已发送回复。如果出现任何错误,将不会调用此方法。这是在 STOR 命令中调用的。” 但是......即使传输被中断结束,也会调用 onUploadEnd 方法。看看这段代码:
public class TestFtplet extends DefaultFtplet{
@Override
public FtpletResult onUploadStart(FtpSession session, FtpRequest request) {
System.out.println("Receiving upload request. Starting...." + request.getCommand());
return FtpletResult.DEFAULT;
}
@Override
public FtpletResult onUploadEnd(FtpSession session, FtpRequest request) {
System.out.println("Ending uploading.... "+ request.getCommand() + request.getArgument() + request.getRequestLine());
System.out.println(": " + session.getUserArgument() + session.toString());
return FtpletResult.DEFAULT;
}
无论上传是否中断,我都有完全相同的输出。有没有办法在转移过程中捕获 ABOR 请求?
我在重写方法中编写了另一个代码beforeComand
(下面的代码在request.getCommand()
STOR 时运行):
System.out.println("\n\n****** RECEIVED UPLOAD REQUEST *******\n\n");
DataConnectionFactory dataConnectionFactory = session.getDataConnection();
try {
System.out.println("Receiving file " + request.getArgument());
temporaryFile = File.createTempFile("FTP_", ".tmp");
OutputStream outputStream = new FileOutputStream(temporaryFile);
DataConnection dataConnection = dataConnectionFactory.openConnection();
session.write(new DefaultFtpReply(150, "Transfering data..."));
dataConnection.transferFromClient(session, outputStream);
System.out.println("Writing file to temporary destination " + temporaryFile.getAbsolutePath());
outputStream.flush();
outputStream.close();
Path source = Paths.get(temporaryFile.getAbsolutePath());
Path destination = Paths.get("C:\\apache-ftpserver-1.0.6\\res\\home\\"+request.getArgument());
if(request.getCommand().toUpperCase().equals("ABOR")) {
System.out.println("Upload interrupted!");
destination = Paths.get("C:\\apache-ftpserver-1.0.6\\res\\unfinished\\"+request.getArgument());
Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Moved file to " + destination);
} else {
Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Done! File moved to " + destination);
}
它成功地将文件写入临时文件夹,但if(request.getCommand().toUpperCase().equals("ABOR"))
始终返回 false,即使 FTP 客户端中断上传并发送 ABOR。有没有办法处理这个?