package org.ponly.fs.ftp;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.ponly.fs.FileSystemException;
import org.ponly.fs.FileView;
import org.ponly.fs.FileSystem;
import org.ponly.fs.util.MonitorInputStream;
import org.ponly.fs.util.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.SocketException;
import static org.ponly.fs.FileSystemException.rethrowFileSystemException;
import static org.ponly.fs.FileView.*;
/**
* 基于 FTP 的文件系统实现
*
* @author vacoor
*/
public class FtpFileSystem implements FileSystem {
private static final Logger LOG = LoggerFactory.getLogger(FtpFileSystem.class);
private static final String CURRENT_DIR = "."; // 当前目录
private static final String PARENT_DIR = ".."; // 上级目录
private static final String DEFAULT_CONTROL_ENCODING = "utf-8";
private static final int DEFAULT_BUFFER_SIZE = 1024 * 1024;
public static final int DEFAULT_PORT = FTP.DEFAULT_PORT;
private String host; // FTP host
private int port; // FTP port
private String username; // FTP username
private String password; // FTP password
private String chroot;
/**
* {@inheritDoc}
*/
@Override
public boolean exists(String path) throws FileSystemException {
return null != stat(path);
}
/**
* {@inheritDoc}
*/
@Override
public FileView stat(String path) throws FileSystemException {
try {
final FTPClient ftp = connect();
try {
return stat(ftp, path);
} finally {
disconnect(ftp);
}
} catch (Throwable ex) {
return rethrowFileSystemException(ex);
}
}
/**
* {@inheritDoc}
*/
@Override
public FileView[] ls(String path) throws FileSystemException {
try {
final FTPClient ftp = connect();
try {
return ls(ftp, path);
} finally {
disconnect(ftp);
}
} catch (Throwable ex) {
return rethrowFileSystemException(ex);
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean mkdirs(String path) throws FileSystemException {
try {
final FTPClient ftp = connect();
try {
return mkdirs(ftp, path);
} finally {
disconnect(ftp);
}
} catch (Throwable ex) {
return rethrowFileSystemException(ex);
}
}
/**
* {@inheritDoc}
*/
@Override
public InputStream open(String file) throws FileSystemException {
return open(file, DEFAULT_BUFFER_SIZE);
}
/**
* 打开给定路径的文件, 如果给定路径不存在返回null, 如果不是文件将抛出异常
* <p/>
* 注: 关闭输入流时才会关闭相关资源, 因此读取完毕后务必关闭输入流
*
* @param file 文件路径
* @param bufferSize 缓冲区大小
* @return 文件输入流或null
* @throws FileSystemException
*/
public InputStream open(String file, int bufferSize) throws FileSystemException {
try {
final FTPClient ftp = connect();
String absolutePath = resolveRemoteAbsolutePath(ftp, file);
try {
FileView stat = stat(ftp, absolutePath);
if (null == stat) {
disconnect(ftp);
return null;
}
if (stat.isDirectory()) {
disconnect(ftp);
throw new FileSystemException("Path " + file + " is a directory.");
}
ftp.allocate(bufferSize);
Path absPath = Path.get(absolutePath);
String pathName = absPath.getName();
String absoluteParent = absPath.getParent().getPath();
// Change to parent directory on the
// server. Only then can we read the
// file
// on the server by opening up an InputStream. As a side effect the working
// directory on the server is changed to the parent directory of the file.
// The FTP client connection is closed when close() is called on the
// FSDataInputStream.
ftp.changeWorkingDirectory(absoluteParent);
InputStream is = ftp.retrieveFileStream(pathName);
InputStream fis = new MonitorInputStream(is) {
@Override
protected void onClosed() throws IOException {
if (!ftp.isConnected()) {
throw new IOException("FTPClient not connected");
}
boolean completed = ftp.completePendingCommand();
disconnect(ftp);
if (!completed) {
throw new IOException("Could not complete transfer, Reply Code - " + ftp.getReplyCode());
}
}
};
if (!FTPReply.isPositivePreliminary(ftp.getReplyCode())) {
// The ftpClient is an inconsistent state. Must close the stream
// which in turn will logout and disconnect from FTP server
fis.close();
throw new FileSystemException("Unable to open file: " + file + ", Aborting");
}
return fis;
} catch (IOException ex) {
disconnect(ftp);
throw ex;
}
} catch (Throwable ex) {
return rethrowFileSystemException(ex);
}
}
/**
* {@inheritDoc}
*/
@Override
public OutputStream create(String file, boolean override) throws FileSystemException {
return create(file, null, override);
}
/**
* {@inheritDoc}
*/
@Override
public OutputStream create(String file, String contentType, boolean override) throws FileSystemException {
return create(file, override, DEFAULT_BUFFER_SIZE);
}
/**
* 获取一个创建文件的输出流并自动创建相关目录, 当 override = false, 如果文件已经存在则抛出异常
*
* @param file 文件路径
* @param override 是否覆盖
* @param bufferSize 缓冲区大小
* @throws FileSystemException
*/
public OutputStream create(String file, boolean override, int bufferSize) throws FileSystemException {
try {
final FTPClient ftp = connect();
String absolutePath = resolveRemoteAbsolutePath(ftp, file);
if (null != stat(ftp, absolutePath)) {
if (!override) {
disconnect(ftp);
throw new FileSystemException("File already exists: " + file);
}
try {
rm(ftp, absolutePath, true, true);
} catch (IOException ex) {
disconnect(ftp);
throw new FileSystemException("Can't override file: " + file);
}
}
Path absPath = Path.get(absolutePath);
String pathName = absPath.getName();
String absoluteParent = absPath.getParent().getPath();
if (null == absoluteParent || !mkdirs(ftp, absoluteParent)) {
absoluteParent = (absoluteParent == n
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本系统使用到的技术有HTML5、CSS、JavaScript、jQuery。界面使用LayUI框架,后台系统使用thymeleaf模版。使用前,请先还原MySQL数据库。项目使用idea开发,因为时间关系,功能并未开发完全,建议有一定基础的人下载使用。后台登录账号:admin 密码:admin123https://blog.csdn.net/mdxiaohu/article/details/81560615
资源推荐
资源详情
资源评论
收起资源包目录
模防CSDN的DBB博客系统。 (1386个子文件)
J2sshMaverickSftpFileSystem.class 17KB
J2sshSftpFileSystem.class 16KB
StorageController.class 16KB
FtpFileSystem.class 15KB
JschSftpFileSystem.class 14KB
StringUtils.class 12KB
IOUtils.class 11KB
LocalFileSystem.class 11KB
AliyunOss.class 11KB
FileSystems.class 10KB
Throwables.class 9KB
Path.class 8KB
Images.class 7KB
FileUtils.class 6KB
Randoms.class 6KB
Base64.class 6KB
LoginServiceImpl.class 5KB
ResourcesController.class 5KB
UserController.class 5KB
Bytes.class 5KB
UEditorController.class 4KB
RoleController.class 4KB
RoleServiceImpl.class 4KB
DownloadView.class 4KB
LoginController.class 4KB
WebConfig.class 4KB
FileView.class 3KB
FenLeiController.class 3KB
ZuodhController.class 3KB
PermissionInterceptor.class 3KB
KaptchaController.class 3KB
PropertiesHandler.class 3KB
ExceptionController.class 2KB
UserServiceImpl.class 2KB
Resources.class 2KB
User.class 2KB
Demo.class 2KB
JSONResponse.class 2KB
Hex.class 2KB
FtpFileSystem$2.class 2KB
MonitorInputStream.class 2KB
Menu.class 2KB
FtpFileSystem$1.class 2KB
J2sshMaverickSftpFileSystem$2.class 1KB
BlogApplication.class 1KB
AliyunOss$1.class 1KB
J2sshSftpFileSystem$2.class 1KB
JschSftpFileSystem$3.class 1KB
PermissionTreeNode.class 1KB
J2sshMaverickSftpFileSystem$1.class 1KB
J2sshSftpFileSystem$1.class 1KB
LayTableResponse.class 1KB
J2sshMaverickSftpFileSystem$SftpChannel.class 1KB
StringUtils$1$1.class 1KB
StringUtils$2$1.class 1KB
AliyunOss$2.class 1KB
ResourcesServiceImpl.class 1KB
J2sshSftpFileSystem$YesHostKeyVerificationImpl.class 1KB
StorageController$2.class 1KB
ResourcesMapper.class 1KB
StorageController$3.class 1KB
FileSystemException.class 1KB
StorageController$NullOutputConsumer.class 1KB
DruidDataSourceFactory.class 1KB
StorageController$1.class 1KB
JschSftpFileSystem$1.class 995B
RoleResources.class 985B
UserMapper.class 983B
LoginService.class 944B
JschSftpFileSystem$2.class 940B
Zuodh.class 896B
FenLei.class 890B
Role.class 873B
UserService.class 870B
JschSftpFileSystem$SftpProgressMonitorAdapter.class 863B
StringUtils$1.class 859B
J2sshSftpFileSystem$SftpChannel.class 851B
StringUtils$2.class 850B
Constants.class 846B
MybatisPlusConfig.class 787B
SuperEntity.class 768B
MybatisPlusConfig.class 766B
SuperServiceImpl.class 728B
FileSystem.class 726B
FenLaiServiceImpl.class 705B
ZuodhServiceImpl.class 702B
BlogApplicationTests.class 620B
MainController.class 585B
RoleService.class 559B
ResourcesService.class 455B
Images$1.class 438B
RoleResourcesMapper.class 322B
FenLeiService.class 305B
ZuodhService.class 303B
FenLeiMapper.class 297B
RoleMapper.class 295B
ZuodhMapper.class 294B
SuperService.class 293B
SuperMapper.class 293B
OssFileSystem.class 155B
共 1386 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
资源评论
- Believe_Zhang2019-05-31哪有前端,骗人鸣珂锵玉2019-06-01你可以直接在文件夹搜索.html文件:dbbBlogSystem\sy-admin\target\classes\templates\uiceng
- TS古宁2019-04-29你好,请问前台和后台的登录地址分别是什么?谢谢!
鸣珂锵玉
- 粉丝: 1258
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功