java 客户端使用smb协议传输windows服务器上的文件


1.业务场景:甲方系统会不定期将一批xls文件存放到windows服务器的共享文件夹下,这些文件将作为本系统的数据来源,需要自动维护到本系统的数据库中。

2.准备工作:

  ①确保windows服务器的smb服务启动,如果未启动,在启用或关闭windows功能中开启

  ②确保存放的目录已经作为共享文件夹

3.代码

        
            jcifs
            jcifs
            1.3.17
        
        
        
            commons-io
            commons-io
            2.6
        
public static void main(String[] args) throws IOException {
    remoteFile(".docx");
    uploadFile("smb://192.168.1.96/myDownload/","C:\\Users\\USERNAME\\Desktop\\orders.txt");
}

public static SmbFile getShareedRoot() throws IOException {
    String remoteShareRoot = "smb://192.168.1.96/myDownload/";
    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("smb://192.168.1.96/",
            "USERNAME", "PASSWORD");
    SmbFile remoteFile = new SmbFile(remoteShareRoot,auth);
    remoteFile.connect(); //尝试连接
    return remoteFile;
}

public static SmbFile getSpecified(String remote) throws IOException {
    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("smb://192.168.1.96/",
            "USERNAME", "PASSWORD");
    SmbFile remoteFile = new SmbFile(remote,auth);
    remoteFile.connect(); //尝试连接
    return remoteFile;
}

//查看共享文件列表
public static SmbFile[] listFiles (SmbFile shareRoot) throws SmbException {
    return shareRoot.listFiles();
}

public static void downloadFile (SmbFile file) throws IOException {
    String fileName = file.getName();
    String localFile = fileName.substring(fileName.lastIndexOf("\\") + 1);
    try(InputStream in = new BufferedInputStream(new SmbFileInputStream(file)); FileOutputStream out  = new FileOutputStream(new File(localFile));){
        byte[] buffer = new byte[4096];
        int len = 0; //读取长度
        while ((len = in.read(buffer, 0, buffer.length)) != - 1) {
            out.write(buffer, 0, len);
        }
        out.flush(); //刷新缓冲的输出流
    }
}

public static void uploadFile (String shareFileRoot, String localFilePath) {
    try {
        File localFile = new File(localFilePath);
        String fileName = localFile.getName();
        SmbFile file = getSpecified(shareFileRoot);
        //当成file用
        if (!file.exists()){
            file.mkdirs();
        }
        //下面一行本来打算想新建File在指定目录下并且指定文件名,后面发现第一个参数与File同方法参数意义不同
        SmbFile remoteFile = new SmbFile( file.getURL() + "/" + fileName);
        IOUtils.copy(new FileInputStream(localFile), new SmbFileOutputStream(remoteFile));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void remoteFile(String want) throws IOException {
    SmbFile shareRoot = getShareedRoot();
    SmbFile smbFile = Arrays.stream(listFiles(shareRoot)).filter(x -> x.getName().endsWith(want)).findAny().get();
    downloadFile(smbFile);
}

TRANSLATE with x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
Bing Webmaster Portal Back