Java从FTP网盘读取文件

这短时间不是很忙了,总结一下自己在开发的道路中遇到的坎坎坷坷,说多了都是泪啊!!!!

FTP,看代码吧!!!

所依赖的pom文件:

<dependency>
	<groupId>org.samba.jcifs</groupId>
	 <artifactId>jcifs</artifactId>
	<version>1.3.3</version>
</dependency>
package com.hsx.demo.utils;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;

/**
 * @author queen
 * @Date: 2019/7/29 15:29
 */
@Slf4j
public class SubFileUtil {

    private String url = "";
    private SmbFile smbFile = null;
    private SmbFileOutputStream smbOut = null;
    private static SubFileUtil smb = null; //共享文件协议

    public static synchronized SubFileUtil getInstance(String url) {
        if (smb == null) {
            return new SubFileUtil(url);
        }
        return smb;
    }

    /**
     * @param url 服务器路径
     */
    private SubFileUtil(String url) {
        this.url = url;
        this.init();
    }

    public void init() {
        try {
            log.info("开始连接...url:" + this.url);
            smbFile = new SmbFile(this.url);
            smbFile.connect();
            log.info("连接成功...url:" + this.url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    /**
     * 上传文件到服务器
     */
    public int uploadFile(File file) {
        int flag = -1;
        BufferedInputStream bf = null;
        try {
            smbOut = new SmbFileOutputStream(this.url + "/" + file.getName(), false);
            bf = new BufferedInputStream(new FileInputStream(file));
            byte[] bt = new byte[8192];
            int n = bf.read(bt);
            while (n != -1) {
                this.smbOut.write(bt, 0, n);
                this.smbOut.flush();
                n = bf.read(bt);
            }
            flag = 0;
            log.info("文件传输结束...");
        } catch (SmbException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
            log.info("找不到主机...url:" + this.url);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != this.smbOut)
                    this.smbOut.close();
                if (null != bf)
                    bf.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return flag;
    }
    
    public static void main(String[] args) {
        //服務器地址 格式為 smb://电脑用户名:电脑密码@电脑IP地址/IP共享的文件夹
        String remoteUrl = "smb://Administrator:[email protected]/gongxiang/";
        String localFile = "C:/Users/Administrator/Desktop/123.txt";  //本地要上传的文件
        File file = new File(localFile);
        SubFileUtil smb = SubFileUtil.getInstance(remoteUrl);
        smb.uploadFile(file);// 上传文件
    }
}

你可能感兴趣的:(项目中的Java问题,程序人生,经验分享)