我想要做的是覆盖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"))
allways返回false,即使FTP Client中断上传并发送ABOR也是如此。
有没有办法解决这个问题?