|
|
@@ -0,0 +1,57 @@
|
|
|
+package com.doc.ftp;
|
|
|
+
|
|
|
+import com.doc.common.constant.Constants;
|
|
|
+import org.apache.ftpserver.ftplet.*;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 自定义事件
|
|
|
+ *
|
|
|
+ * @author wukai
|
|
|
+ */
|
|
|
+public class DocFtpLet extends DefaultFtplet {
|
|
|
+
|
|
|
+ public static final Logger log = LoggerFactory.getLogger(DocFtpLet.class);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 开始上传
|
|
|
+ * Override this method to intercept uploads
|
|
|
+ *
|
|
|
+ * @param session The current {@link FtpSession}
|
|
|
+ * @param request The current {@link FtpRequest}
|
|
|
+ * @return The action for the container to take
|
|
|
+ * @throws FtpException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public FtpletResult onUploadStart(FtpSession session, FtpRequest request)
|
|
|
+ throws FtpException, IOException {
|
|
|
+ //获取上传文件的上传路径
|
|
|
+ String path = session.getUser().getHomeDirectory();
|
|
|
+ try {
|
|
|
+ //自动创建上传路径
|
|
|
+ Files.createDirectories(Paths.get(path));
|
|
|
+ } catch (IOException ignored) {
|
|
|
+ //不用管,表示已经有该目录
|
|
|
+ }
|
|
|
+ //获取上传用户
|
|
|
+ String name = session.getUser().getName();
|
|
|
+ //获取上传文件名
|
|
|
+ String filename = request.getArgument();
|
|
|
+ String fileType = filename.substring(filename.lastIndexOf("."));
|
|
|
+ String allowType = Constants.IMAGE_EXTENSION + ".pdf";
|
|
|
+ //只允许图片和PDF
|
|
|
+ if (allowType.contains(fileType)) {
|
|
|
+ log.info("用户:'{}',上传文件到目录:'{}',文件名称为:'{}',状态:开始上传~", name, path, filename);
|
|
|
+ return super.onUploadStart(session, request);
|
|
|
+ } else {
|
|
|
+ log.error("上传非法文件{}", name);
|
|
|
+ throw new IOException();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|