加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Ftp与NFS服务器端上传和下载

发布时间:2020-12-14 23:17:56 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 本功能实现FTP和NFS服务器上的文件传输,应用Apache提供的 方法 commons-vfs2工具 import java.io.File; import org.apache.commons.io.FilenameUtils

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

本功能实现FTP和NFS服务器上的文件传输,应用Apache提供的 方法 commons-vfs2工具
import java.io.File;
 
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.Selectors;
import org.apache.commons.vfs2.impl.StandardFileSystemManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
 
public class DownOrUpLoad {
   public static Logger logger = LoggerFactory.getLogger(DownOrUpLoad.class);
 
/**
     * 上传文件到本地路径
     * 
     * @param remotePath 远程服务器上的路径
     * @param tempApkPath 本地路径
     */
   private void uploadAPk(final String tempApkPath,final String remotePath){
        switch (protocols) {
        case  "nfs" :
            uploadAPk_nfs(tempApkPath,remotePath);
            break;
        case "ftp" :
            uploadAPk_ftp(tempApkPath,remotePath);
            break;
        default:
            break;
        }    
    }
     
    private void uploadAPk_ftp(final String tempApkPath,final String remotePath){
        logger.info("upload file {} to {} ",tempApkPath,remotePath);
        try{
            final String ftpConnect=ftpPath;
            StandardFileSystemManager fsManager = new StandardFileSystemManager();
            fsManager.init();
             
            FileObject destFile = fsManager.resolveFile(ftpConnect+remotePath);
             
            if(destFile.exists()){
                destFile.createFile();
            }
            FileObject src = fsManager.resolveFile(tempApkPath);
            destFile.copyFrom(src,Selectors.SELECT_FILES);
             
            src.close();destFile.close();fsManager.close();
             
        }catch(Exception e){
            e.printStackTrace();
        }
    }
     
    private void uploadAPk_nfs(final String tempApkPath,remotePath);
        try {
            FileUtils.copyFile(new File(tempApkPath),new File(nfsPath+remotePath));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
     
    /**
     * 下载远程服务器apk到本地路径
     * 
     * @param remoteApkPath 远程服务器上的路径
     * @return 本地路径
     */
    private String downloadAPKtoLocalTempPath(final String remoteApkPath){
        switch (protocols) {
        case  "nfs" :
            return downloadAPK_NFS(remoteApkPath);
        case "ftp" :
            return downloadAPk_FTP(remoteApkPath);
        default:
            return "";
        }    
    }
     
    private  String downloadAPK_NFS(String remoteApkPath){
        File srcFile=new File(remoteApkPath);
        File descFile=new File(apkLocalPath+FilenameUtils.getName(remoteApkPath));
        try {
            if(srcFile.exists()){
                FileUtils.copyFile(srcFile,descFile);
                return descFile.getAbsolutePath();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
         
         
    }
     
    private  String downloadAPk_FTP(final String remoteApkPath){
        try{
            final String ftpConnect=ftpPath;
            StandardFileSystemManager fsManager = new StandardFileSystemManager();
            fsManager.init();
            File descFile=new File(apkLocalPath+FilenameUtils.getName(remoteApkPath));
            FileObject dest = fsManager.resolveFile(descFile.getAbsolutePath());
            if(dest.exists()){
                dest.createFile();
            }
            FileObject src = fsManager.resolveFile(ftpConnect+remoteApkPath);
            dest.copyFrom(src,Selectors.SELECT_FILES);
             
            src.close();dest.close();fsManager.close();
             
            return descFile.getAbsolutePath();
        }catch(Exception e){
            e.printStackTrace();
        }
         
        return "";
         
    }
     
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读