KKFileView(五)——文件上传

2021SC@SDUSC

该项目的文件在预览之前需要上传至服务器的本地文件夹,因此有工具类downloadutils

具体流程为 

一、构造描述

fileAttribute文件描述,包含属性有
FileType type, String suffix, String name, String url, String officePreviewType
filename文件名
ReturnResponse<String> 
返回类型为文件的绝对路径
public static ReturnResponse<String> downLoad(FileAttribute fileAttribute, String fileName) {
        String urlStr = fileAttribute.getUrl();
        ReturnResponse<String> response = new ReturnResponse<>(0, "下载成功!!!", "");
        String realPath = DownloadUtils.getRelFilePath(fileName, fileAttribute);

二.获取文件真实路径

简单来说就是在文件名的后面加上.类型,并在前面添上他的路径,返回的就是文件的真实路径了

private static String getRelFilePath(String fileName, FileAttribute fileAttribute) {
        String type = fileAttribute.getSuffix();
        if (null == fileName) {
            UUID uuid = UUID.randomUUID();
            fileName = uuid + "." + type;
        } else { // 文件后缀不一致时,以type为准(针对simText【将类txt文件转为txt】)
            fileName = fileName.replace(fileName.substring(fileName.lastIndexOf(".") + 1), type);
        }
        String realPath = fileDir + fileName;
        File dirFile = new File(fileDir);
        if (!dirFile.exists() && !dirFile.mkdirs()) {
            logger.error("创建目录【{}】失败,可能是权限不够,请检查", fileDir);
        }
        return realPath;
    }

 三. 判断文件来源,ftp/http

若来源于http,调用FileUtils中的copyUrlToFile方法 => 此方法URL对应的文件复制一份

 if (isHttpUrl(url)) {
                File realFile = new File(realPath);
                FileUtils.copyURLToFile(url,realFile);
            }

若来源于ftp,构造ftputils类,调用其中的download方法,主要通过ftpclient进行文件的读写,FTPClient 封装了从 FTP 服务器存储和检索文件所需的所有功能。此类负责与 FTP 服务器交互的所有低级细节,并提供方便的高级接口。

与所有派生自 的类一样SocketClientconnect 在做任何事情之前,必须首先连接到服务器 ,最后是 disconnect 在完全完成与服务器的交互之后。然后需要检查FTP回复码,看是否连接成功

以下是代码中检测是否连接成功的代码

String username = StringUtils.isEmpty(ftpUsername) ? ConfigConstants.getFtpUsername() : ftpUsername;
        String password = StringUtils.isEmpty(ftpPassword) ? ConfigConstants.getFtpPassword() : ftpPassword;
        String controlEncoding = StringUtils.isEmpty(ftpControlEncoding) ? ConfigConstants.getFtpControlEncoding() : ftpControlEncoding;
        URL url = new URL(ftpUrl);
        String host = url.getHost();
        int port = (url.getPort() == -1) ? url.getDefaultPort() : url.getPort();
        String remoteFilePath = url.getPath();
        LOGGER.debug("FTP connection url:{}, username:{}, password:{}, controlEncoding:{}, localFilePath:{}", ftpUrl, username, password, controlEncoding, localFilePath);
        FTPClient ftpClient = connect(host, port, username, password, controlEncoding);

连接成功后,正式进入下载,同样使用的是OutputStream类控制字节流

OutputStream outputStream = new FileOutputStream(localFilePath);
        ftpClient.enterLocalPassiveMode();
        boolean downloadResult = ftpClient.retrieveFile(new String(remoteFilePath.getBytes(controlEncoding), StandardCharsets.ISO_8859_1), outputStream);
        LOGGER.debug("FTP download result {}", downloadResult);
        outputStream.flush();
        outputStream.close();

然后需要推出ftp的登录并且关闭与ftp的连接

ftpClient.logout();
ftpClient.disconnect();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值