zoukankan      html  css  js  c++  java
  • 同一个局域网内,使用 java 从服务器共享文件夹中复制文件到本地。

    1 引用jar 包

    <dependency>
        <groupId>org.samba.jcifs</groupId>
        <artifactId>jcifs</artifactId>
        <version>1.3.14-kohsuke-1</version>
    </dependency>

    2 从本地上传文件到服务器共享文件夹

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import jcifs.smb.SmbFile;
    import jcifs.smb.SmbFileOutputStream;
    
    /**
     *
     * <b>类名称:</b>CopyFileToLan<br/>
     * <b>类描述:</b> 本地文件写入局域网共享文件夹   <br/>
     * <b>版本:</b>V1.0<br/>
     */
    public class CopyFileToLan {
    
        public static void main(String[] args){
    
            InputStream in = null;
            OutputStream out = null;
            try {
                //测试文件
                File localFile = new File("D:\文档\test.txt");
    
                String host = "192.168.1.151";//远程服务器的地址
                String username = "admin";//用户名
                String password = "admin";//密码
                String path = "/share/";//远程服务器共享文件夹名称
    
                String remoteUrl = "smb://" + username + ":" + password + "@" + host + path + (path.endsWith("/") ? "" : "/");
                SmbFile remoteFile = new SmbFile(remoteUrl + "/" + localFile.getName());
                remoteFile.connect();
    
                in = new BufferedInputStream(new FileInputStream(localFile));
                out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
    
                byte[] buffer = new byte[4096];
                int len = 0;
                while ((len = in.read(buffer, 0, buffer.length)) != -1) {
                    out.write(buffer, 0, len);
                }
                out.flush();
            }
            catch (Exception e) {
                String msg = "发生错误:" + e.getLocalizedMessage();
                System.out.println(msg);
            }
            finally {
                try {
                    if(out != null) {
                        out.close();
                    }
                    if(in != null) {
                        in.close();
                    }
                }
                catch (Exception e) {}
            }
        }
    }

    3 从服务器共享文件夹下载文件到本地

    import jcifs.smb.SmbFile;
    import jcifs.smb.SmbFileInputStream;
    import java.io.*;
    
    /**
     * <b>类名称:</b>CopyLanFileToLocal<br/>
     * <b>类描述:</b> 读取局域网共享文件夹文件,到本地文件夹   <br/>
     */
    public class CopyLanFileToLocal {
    
        public static void main(String[] args) {
            InputStream in = null;
            OutputStream out = null;
            try {
                //目标文件名
                String fileName = "1.jpg";
                //本地文件
                String localPath = "d:/";
    
                String host = "192.168.1.151";//远程服务器的地址
                String username = "admin";//用户名
                String password = "admin";//密码
                String path = "/share/";//远程服务器共享文件夹名称
    
                String remoteUrl = "smb://" + username + ":" + password + "@" + host + path + (path.endsWith("/") ? "" : "/");
    
                //创建远程文件对象
                SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName);
                remoteFile.connect();
    
                //创建文件流
                in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
                out = new BufferedOutputStream(new FileOutputStream(new File(localPath + fileName)));
                //读取文件内容
                byte[] buffer = new byte[4096];
                int len = 0;
                while ((len = in.read(buffer, 0, buffer.length)) != -1) {
                    out.write(buffer, 0, len);
                }
    
                out.flush();
            } catch (Exception e) {
                String msg = "下载远程文件出错:" + e.getLocalizedMessage();
                System.out.println(msg);
            } finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception e) {
                }
            }
        }
    }

    SmbFile 和 FIle 对文件的操作基本一致, 在文件复制的时候,想过用 commons-io 的FileUtils.copyFile(final File srcFile, final File destFile);和 java 1.7 的 Files.copy(Path source, Path target, CopyOption... options);但是由于SmbFile 和 File 不属于一个类型,导致失败,最后只能选择使用流的方式进行操作。

  • 相关阅读:
    【消息队列MQ】各类MQ比较
    MySql查询功能梳理
    头条日常实习生面经 2018.11.28
    排序算法 JavaScript
    浅谈二分查找 JavaScript
    LeetCode17.电话号码的字母组合 JavaScript
    LeetCode16.最接近的三数之和 JavaScript
    LeetCode15.三数之和 JavaScript
    LeetCode14.最长公共前缀 JavaScript
    LeetCode13.罗马数字转整数 JavaScript
  • 原文地址:https://www.cnblogs.com/zwb1234/p/9257584.html
Copyright © 2011-2022 走看看