zoukankan      html  css  js  c++  java
  • java基于jcifs.smb实现远程发送文件到服务器

    1、服务器指定共享文件夹

    1.1、验证服务器共享文件夹本地可以访问:

     2、导入依赖的相关jar包         jcifs-1.3.**.jar:

            <dependency>
                <groupId>jcifs</groupId>
                <artifactId>jcifs</artifactId>
                <version>1.3.17</version>
            </dependency>

    3.创建java类:SmbUtil

    并粘贴下面的代码:

    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.UnknownHostException;
    
    import jcifs.smb.SmbException;
    import jcifs.smb.SmbFile;
    import jcifs.smb.SmbFileOutputStream;
    
    public class SmbUtil {
        // 1. 声明属性
        private String url = "smb://userName:password@192.168.2.153/mars/";
        private SmbFile smbFile = null;
        private SmbFileOutputStream smbOut = null;
        private static SmbUtil smbUtil = null; // 共享文件协议
    
        private SmbUtil(String url) {
            this.url = url;
            this.init();
        }
    
        // 2. 得到SmbUtil和连接的方法
        public static synchronized SmbUtil getInstance(String url) {
            if (smbUtil == null)
                return new SmbUtil(url);
            return smbUtil;
        }
    
    
        // 3.smbFile连接
        public void init() {
            try {
                System.out.println("开始连接...url:" + this.url);
                smbFile = new SmbFile(this.url);
                smbFile.connect();
                System.out.println("连接成功...url:" + this.url);
            } catch (MalformedURLException e) {
                e.printStackTrace();
                System.out.print(e);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.print(e);
            }
        }
    
        // 4.上传文件到服务器
        public int uploadFile(File file) {
            int flag = -1;
            BufferedInputStream bf = null;
            try {
                this.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;
                System.out.println("文件传输结束...");
            } catch (SmbException e) {
                e.printStackTrace();
                System.out.println(e);
            } catch (MalformedURLException e) {
                e.printStackTrace();
                System.out.println(e);
            } catch (UnknownHostException e) {
                e.printStackTrace();
                System.out.println("找不到主机...url:" + this.url);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println(e);
            } finally {
                try {
                    if (null != this.smbOut)
                        this.smbOut.close();
                    if (null != bf)
                        bf.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
    
            return flag;
        }
    
        // 5. 在main方法里面测试
        public static void main(String[] args) {
            // 服務器地址 格式為 smb://电脑用户名:电脑密码@电脑IP地址/IP共享的文件夹
            String remoteUrl = "smb://administrator:123456@10.8.66.22/mars/";
            String localFile = "C:\Users\Administrator\Desktop\***.txt"; // 本地要上传的文件
            File file = new File(localFile);
            SmbUtil smb = SmbUtil.getInstance(remoteUrl);
            smb.uploadFile(file);// 上传文件
        }
    }
  • 相关阅读:
    用任务计划管理计划任务对付任务计划-禁止WPS提示升级
    破解激活Win10无风险?激活后删除激活工具无影响===http://www.pconline.com.cn/win10/693/6932077_all.html#content_page_4
    VS2013 密钥 – 所有版本
    2017面试题1
    模拟锚点
    输入框被软键盘遮
    资源(GitHub)
    全国城市部分js
    subline3 插件
    app下载——js设备判断
  • 原文地址:https://www.cnblogs.com/luzhanshi/p/12067186.html
Copyright © 2011-2022 走看看