一、导入包(maven中央仓库https://mvnrepository.com/artifact/org.samba.jcifs/jcifs/1.3.3)

<!-- jcifs 共享文件夹目录 -->
<dependency><groupId>org.samba.jcifs</groupId><artifactId>jcifs</artifactId><version>1.3.3</version>
</dependency>

二、工具类


import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;import java.io.*;public class shareSmbFileUtil {private static String USER_DOMAIN = "ip";private static String USER_ACCOUNT = "username";private static String USER_PWS = "password";/*** @Title smbGet* @Param shareUrl 共享目录中的文件路径,如smb://192.168.1.158/share/test.txt* @Param localDirectory 本地目录,如tempStore/smb*/public static void smbGet(String shareUrl, String localDirectory) throws Exception {NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(USER_DOMAIN, USER_ACCOUNT,USER_PWS);SmbFile remoteFile = new SmbFile(shareUrl, auth);if (!remoteFile.exists()) {System.out.println("共享文件不存在");return;}// 有文件的时候再初始化输入输出流InputStream in = null;OutputStream out = null;System.out.println("下载共享目录的文件 shareUrl 到 localDirectory");try {String fileName = remoteFile.getName();File localFile = new File(localDirectory + File.separator + fileName);File fileParent = localFile.getParentFile();if (null != fileParent && !fileParent.exists()) {fileParent.mkdirs();}in = new BufferedInputStream(new SmbFileInputStream(remoteFile));out = new BufferedOutputStream(new FileOutputStream(localFile));byte[] buffer = new byte[1024];while (in.read(buffer) != -1) {out.write(buffer);buffer = new byte[1024];}out.flush(); //刷新缓冲区输出流} catch (Exception e) {e.printStackTrace();} finally {out.close();in.close();}}/**** @Title smbPut* @Description 向共享目录上传文件* @Param shareDirectory 共享目录* @Param localFilePath 本地目录中的文件路径* @date 2019-01-10 20:16*/public static void smbPut(String shareDirectory, String localFilePath,String userDomain,String userAccount,String userPassword) {InputStream in = null;OutputStream out = null;try {NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(userDomain, userAccount,userPassword);File localFile = new File(localFilePath);String fileName = localFile.getName();SmbFile remoteFile = new SmbFile(shareDirectory + "/" + fileName,auth);in = new BufferedInputStream(new FileInputStream(localFile));out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));byte[] buffer = new byte[1024];while (in.read(buffer) != -1) {out.write(buffer);buffer = new byte[1024];}out.flush();} catch (Exception e) {e.printStackTrace();} finally {try {out.close();in.close();} catch (IOException e) {e.printStackTrace();}}}public static void main(String[] args) throws Exception {// remoteUrl 格式示例 【smb://132.20.2.33/CIMPublicTest/】// 目录String shareDir = "smb://192.168.1.158/share/";String localDir = "D://ht/file/PDF/test.pdf";smbPut(shareDir,localDir,USER_DOMAIN,USER_ACCOUNT,USER_PWS);
//        listFiles(shareDir);}/*** @Title listFiles* @Description 遍历指定目录下的文件* @date 2019-01-11 09:56*/private static void listFiles(String shareDirectory) throws Exception {long startTime = System.currentTimeMillis();// 域服务器验证NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(USER_DOMAIN, USER_ACCOUNT,USER_PWS);SmbFile remoteFile = new SmbFile(shareDirectory, auth);System.out.println("远程共享目录访问耗时:【{}】" + (System.currentTimeMillis() - startTime));if (remoteFile.exists()) {SmbFile[] files = remoteFile.listFiles();
//            remoteFile.listFiles(shareDirectory);for (SmbFile f : files) {System.out.println(f.getName());}} else {System.out.println("文件不存在");}}
}

JAVA操作共享文件夹文件、下载、读取(windows、Linux通用)相关推荐

  1. 【C#】操作局域网共享文件夹文件,将本地文件复制到共享文件夹

    前言 最近,做一个功能,需要将本地文件复制到局域网共享文件夹中. 访问共享文件夹,一般需要共享电脑的用户名和密码,可以使用cmd.exe来操作文件. 源码 下面整理了一个局域网操作文件的类,仅供参考 ...

  2. java从远程url文件流读取文件并下载到本地

    java从远程url文件流读取文件,且下载到本地,写个循环可实现批量 import java.io.*; import java.net.HttpURLConnection; import java. ...

  3. 操作系统镜像下载【windows+linux+mac】

    操作系统镜像下载[windows+linux+mac] 1.windows各版本镜像下载 https://msdn.itellyou.cn/ 2.Windows系统激活工具[破解软件往往报毒,选择使用 ...

  4. java 通过 SmbFile 类操作共享文件夹(1)

    四.上传文件 /** 上传文件 @param remoteUrl @param shareFolderPath @param localFilePath @param fileName */ publ ...

  5. ubuntu共享文件夹文件看不到_实验08:轻松搭建文件夹共享

    一.[实验目的] 1.实现简单的文件夹共享 2. 实现启用密码保护访问文件夹共享: 3.简单实现FTP文件共享的配置: 二.[实验器材] 两台物理机安装windows7系统环境: 三.[实验步骤] 任 ...

  6. linux共享文件夹 redhat_Linux怎么访问Windows共享文件夹

    请关注本头条号,每天坚持更新原创干货技术文章. 如需学习视频,请在微信搜索公众号"智传网优"直接开始自助视频学习 1. 前言 本文主要讲解如何在Linux系统上访问Windows共 ...

  7. Java操作word模板文件

    关于导出word文档,之前想过用ireport但模板文件比较复杂不容易画.所以采取了Java操作word文件,替换word中的元素方法 模板文件如下 单位名称:$ACCTNAME$ NO: $SN$ ...

  8. php中配置文件操作config.php文件的读取修改等操作

    定义和用法 fputs() 函数写入文件(可安全用于二进制文件). fputs() 函数是 fwrite() 函数的别名. //file 必需.规定要写入的打开文件. //string 必需.规定要写 ...

  9. 算法-(java)-从文件中读取、写入数据

    1.m的n次幂表示 在算法题中经常遇到10的n次幂,java中计算中,有一个函数,返回double类型,math.pow(m,n),m为基数,n为幂次方.这样打印出结果,会打印出带e的数字,如果想要实 ...

  10. linux 内网共享文件夹_彻底搞定Linux局域网共享 | 薄荷开源网

    此前,由于以使用公司电脑为主,个人下载的大量视频.ISO镜像等大文件都保存在公司电脑上.近期为了迁移到私人电脑上大费周章.由于没有大容量的移动硬盘,只能考虑使用局域网共享来传输文件. 可是公司电脑上的 ...

最新文章

  1. mediawiki安装使用
  2. vue的父子组建之间的通信(-),基于props和$emit之间的传递
  3. java 151建议_编写高质量代码改善java程序的151个建议——导航开篇
  4. AC日记——最大子段和 洛谷 P1115
  5. ios集成firebase_如何使用Firebase将Google Login集成到Ionic应用程序中
  6. python xgboost安装_win7 64 python2 xgboost安装
  7. k8s核心技术-资源编排(yaml)的编写方式---K8S_Google工作笔记0019
  8. LXC源码编译测试(五)
  9. CSDN的积分如何获取(转)
  10. Java程序员面试简历模板
  11. Google Guice简介
  12. MEGA使用VCF/Hapmap数据构建进化树
  13. ERROR: Pool overlaps with other one on this address space 解决办法
  14. 华为手机html乱码,华为手机语言设置中文
  15. Tomcat、Servlet、JSP、EL和JDK版本对照以及各版本Tomcat下载
  16. C++OpenCV系统学习(17)——图像分割与抠图(4)Grabcut
  17. 如何简单又快速的清理C盘内存
  18. 怀化学院计算机不过能毕业吗,面对困境,你会如何选择?
  19. 基于MFC的平行投影算法
  20. 【两万字】面试官:听说你很懂集合源码,接我二十道问题!

热门文章

  1. Elasticsearch短语或近似匹配及召回率案例深入剖析-搜索系统线上实战
  2. Ubuntu14.10 更新源,现在很多源已经停用了建议使用ubuntu15以上
  3. laravel.log could not be opened 解决方法
  4. 2018年北京信息科技大学第十届程序设计竞赛暨ACM选拔赛 C 颜料的混合 (计算几何)
  5. 解除手机莫明其妙定制的短信业务
  6. Gateway配置介绍
  7. python之面向对象基础一
  8. win10笔记本电脑外接USB键盘开机失灵解决方案
  9. 8086CPU I/O系统组织 8253芯片 8255A芯片
  10. 赶紧收藏!41个Web UI工具包资源免费及付费下载