java访问windows远端共享文件的方法
没密码的,直接用url访问就可以了,跟本地文件相同。
有密码的,用smb。
远端需要开启smb。
win10启动smb的方法:
https://jingyan.baidu.com/article/47a29f2477fb56c014239996.html
注意,配完必须要重启windows。
smb需要在服务器端启动,也就是那台windows机器,不启动会报错 Software caused connection abort: recv failed
package file;
import jcifs.smb.*;
import java.io.*;
public class ShareFile {
/**
* 一个普通的读取远端共享文件的方法,不需要输入密码
* @param sourceFilePath
* @param encode
* @throws IOException
*/
public static void readFile(String sourceFilePath, String encode) throws IOException {
File file = new File(sourceFilePath);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encode));
StringBuilder strBuilder = new StringBuilder();
String sLine = null;
while ((sLine = br.readLine()) != null) {
strBuilder.append(sLine);
strBuilder.append("\r\n");
}
br.close();
System.out.println(strBuilder.toString());
}
/**
* 一个普通的读取远端共享文件并保存到本地的方法,不需要输入密码
* @param srcFileUrl
* @param dstFilePath
* @param dstFileName
* @throws IOException
*/
public static void readAddSaveFile(String srcFileUrl, String dstFilePath,String dstFileName) throws IOException {
File dstPath = new File(dstFilePath);
if (!dstPath.exists()) {
dstPath.mkdirs();
}
OutputStream outputStream = new FileOutputStream(dstPath.getPath() + File.separator + dstFileName);
// 开始读取
int len;
byte[] bs = new byte[1024]; // 1K的数据缓冲
File srcFile = new File(srcFileUrl);
InputStream inputStream = new FileInputStream(srcFile);
while ((len = inputStream.read(bs)) != -1) {
outputStream.write(bs, 0, len);
}
inputStream.close();
outputStream.flush();
outputStream.close();
}
/**
* 一个往远端smb文件写数据的方法。
* @throws IOException
*/
public static void getFileBySmb() throws IOException {
String user = "Felicia_shi";
String pass = "123456";
String sharedFolder = "Share";
String path = "smb://1.1.1.109/" + sharedFolder + "/test.txt";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", user, pass);
SmbFile smbFile = new SmbFile(path, auth);
System.out.println(smbFile.getDfsPath());
;
SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
smbfos.write("testing.and writing to a file".getBytes());
System.out.println("completed nice !");
}
/**
* 远端文件路径。账号密码可以用这个url里面配置的,也可以用上面的NtlmPasswordAuthentication方式
*/
private static String smbUrl = "smb://Felicia_shi:123456@1.1.1.109/Share";
/**
* 在远端smb路径下创建路径
* @param dir
* @throws Exception
*/
public static void createDir(String dir) throws Exception{
SmbFile fp = new SmbFile(smbUrl +"/"+dir);
System.out.println("fieldir+++++++++++++++++++++="+ smbUrl +"/"+dir);
//File fp = new File("Z://"+dir);
// 目录已存在创建文件夹
if (fp.exists() && fp.isDirectory()) {
} else{
// 目录不存在的情况下,会抛出异常
fp.mkdir();
}
}
/**
* 拷贝smb下一个文件到本地。dstFileName为本地带文件名的路径,smbUrl为远端路径,文件名一样。
* 这么写比较死,可以把文件名和路径分开放。
* @param dstFileName
*/
public static void copyDir(String dstFileName) {
InputStream in = null;
OutputStream out = null;
try{
File fp = new File(dstFileName);
SmbFile remoteFile = new SmbFile(smbUrl +"//"+fp.getName());
System.out.println("remoteFile+++++++++++++++++++++="+remoteFile);
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
out = new BufferedOutputStream(new FileOutputStream(fp));
// in = new BufferedInputStream(new FileInputStream(fp));
// out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
// 刷新此缓冲的输出流
out.flush();
}catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 获取本段路径下的所有文件名
* @param path
* @return
*/
public static String[] getFileNames(String path) {
File dirFile = new File(path);
if (dirFile.isDirectory()) {
File[] files = dirFile.listFiles();
String[] fileNames = new String[files.length];
for (int i = 0; i < files.length; i++) {
fileNames[i] = files[i].getAbsolutePath();
}
return fileNames;
} else {
return null;
}
}
public static void main(String[] args) {
// //读取单个文件
// try {
// ShareFile.readFile("\\\\1.1.1.110\\sfx\\gx\\11.txt","utf-8");
// } catch (IOException e) {
// e.printStackTrace();
// }
// //读取某个目录下所有文件
//
// String[] fileNames = getFileNames("\\\\1.1.1.110\\sfx\\gx");
// String encode = "utf-8";
// for (String fileName : fileNames) {
// try {
// readFile(fileName, encode);
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// try {
// readAddSaveFile("\\\\1.1.1.110\\sfx\\gx\\a.pdf","\\\\1.1.1.110\\sfx\\gx1","a.pdf");
// } catch (IOException e) {
// e.printStackTrace();
// }
// try {
// getFileBySmb();
// createDir("111");
// } catch (IOException e) {
// e.printStackTrace();
// } catch (Exception e) {
// e.printStackTrace();
// }
copyDir("E:\\\\sfx\\gx1\\a.pdf");
}
}
java访问windows远端共享文件的方法的更多相关文章
- 双系统Ubuntu无法访问windows磁盘分区解决方法
为了更好的体验各种操作系统,在电脑中安装双系统是很好的选择,但在使用中难免会遇到这样或那样的问题. 最近总是遇到Ubuntu系统下无法访问windows磁盘分区问题,看了系统日志发现是挂载磁盘出问题了 ...
- Linux 中 java 访问 windows共享目录
有两个方案: 1.将windows共享目录,挂载到linux系统下,通过使用本地目录访问windows共享目录 2.通过samba的java实现包,不过需要开个windows共享目录的账户 http ...
- Linux访问Windows共享目录的方法——smbclient
之前写过Ubuntu访问Windows共享目录,里面主要介绍如何在GUI上访问windows共享目录. 本文侧重于整理通过命令行访问的方法. 先确保smbclient.ubuntu下可以直接运行下面命 ...
- Mac下访问windows的共享文件夹
Finder->前往->smb://<user>@<ip>
- linux访问windows共享文件夹的方法
博客转自:http://www.01happy.com/linux-access-windows-shares-folders/ 有时需要在linux下需要访问windows的共享文件夹,可以使用mo ...
- linux访问windows共享文件夹的两种方法
有时需要在linux下需要访问windows的共享文件夹,可以使用mount挂载或者使用samba连接. 1.mount挂载 首先创建被挂载的目录: $ mkdir windows 将共享文件夹挂载到 ...
- (转)linux访问windows共享文件夹的两种方法
有时需要在linux下访问window的共享文件,可以使用mount挂载或使用samba连接. 1,mount挂载 $ mkdir windows 将共享文件夹挂载到windows文件夹: mount ...
- Java读写Windows共享文件夹 .
版权声明:本文为博主原创文章,未经博主允许不得转载. 项目常常需要有访问共享文件夹的需求,例如共享文件夹存储照片.文件等.那么如何使用Java读写Windows共享文件夹呢? Java可以使用JCIF ...
- 【转】Mac访问Windows共享文件夹
相信大多数的用户用Windows访问Windows的共享文件夹是一件很容易的事,但是如果用Mac来访问Windows共享文件夹就会遇到很多的麻烦了,尤其是设置是比较有区别的吗,接下来的将用图文交大家怎 ...
随机推荐
- 案例25-servlet的抽取
1 product模块的抽取版本一 1 ProductServlet代码 抽取之后,原来对应的IndexServlet,ProductListByCidServlet等都可以删除.对应的web.xml ...
- 深入java虚拟机学习 -- 类的卸载
类的生命周期 在开始本节之前让我们再来回顾下类的生命周期 没看过前6个过程的同学建议从头看下<深入java虚拟机学习 -- 类的加载机制>,这里就不再过多介绍了,着重说下类的卸载 类的卸载 ...
- org.dbunit.dataset.NoSuchTableException: t_group
遇到这个错误发现异常信息里有一项是这个 15:30:10,609 ERROR SchemaUpdate:236 - HHH000388: Unsuccessful: create table t_gr ...
- 【linux】虚拟机内装Linux系统的ssh访问
一般在虚拟机内安装一个Linux系统,虚拟机网络设置为桥接后,Linux系统会在安装的过程中自动设置其为dhcp配置,会给其随机分配一个ip,这个ip可以用命令 "ifconfig" ...
- 深入redis内部--初始化服务器
初始化服务器代码如下: void initServer() { int j; signal(SIGHUP, SIG_IGN); signal(SIGPIPE, SIG_IGN); setupSigna ...
- Swift编程权威指南第2版 读后收获
自从参加工作一直在用OC做iOS开发.在2015年的时候苹果刚推出swift1.0不久,当时毕竟是新推出的语言,大家也都很有激情的学习.不过在学完后发现很难在实际项目中使用,再加上当时公司项目都是基于 ...
- log4j 详细讲解
日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方便的日志记录.在apache网站:jakarta.apache.org/log4j 可以免费下载到Log ...
- angular 之 jquery-autocomplete
自动完成还是原来的 bassistance.de 好用,详细用法参考官网. angular的js paths配置及依赖关系不多说: 'jquery': 'static/jquery-1.11.3/jq ...
- ,SQL语句关键词以及实例
1.select:功能:查找,语法:select 列名 from 表名(注:可以一次从一个表中查询多个列或者从多个表名中查询资料) 实例:select Name from Table1,返回Table ...
- 一台电脑上运行两个tomcat
1.建立两个文件夹,tomcat1,tomcat2,分别在里面放入tomcat7文件(非安装版) 2.改配置 tomcat1中的配置就不用改了,直接用默认配置 tomcat2中的配置要改要,改conf ...