同一个局域网内,使用 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 不属于一个类型,导致失败,最后只能选择使用流的方式进行操作。
同一个局域网内,使用 java 从服务器共享文件夹中复制文件到本地。的更多相关文章
- C#下载局域网共享文件夹中的文件
在公司的局域网内部,有个主机,共享了几个文件夹给下面的客户机使用. 想要利用这个文件夹上传最新的winform程序版本,每次运行exe的时候检测局域网的软件版本达到更新exe的目的. 这里有个例子,是 ...
- 在Intellij上开发项目发布到tomcat时,同一个局域网内的其他机子访问不到自己电脑上tomcat中的项目,只能本机访问
在Intellij上开发项目发布到tomcat时,同一个局域网内的其他机子访问不到自己电脑上tomcat中的项目,只能本机访问 问题描述:在Intellij上开发项目发布到tomcat时,同一个局域网 ...
- Java读写Windows共享文件夹 .
版权声明:本文为博主原创文章,未经博主允许不得转载. 项目常常需要有访问共享文件夹的需求,例如共享文件夹存储照片.文件等.那么如何使用Java读写Windows共享文件夹呢? Java可以使用JCIF ...
- java io流(字节流)复制文件
java io流(字节流) 复制文件 //复制文件 //使用字节流 //复制文本文件用字符流,复制其它格式文件用字节流 import java.io.*; public class Index{ pu ...
- Java学习-040-级联删除目录中的文件、目录
之前在写应用模块,进行单元测试编码的时候,居然脑洞大开居然创建了一个 N 层的目录,到后来删除测试结果目录的时候,才发现删除不了了,提示目录过长无法删除.网上找了一些方法,也找了一些粉碎机,都没能达到 ...
- java基础 File 递归删除文件夹中所有文件文件夹 目录(包含子目录)下的.java文件复制到e:/abc文件夹中, 并统计java文件的个数
File 递归删除文件夹中所有文件文件夹 package com.swift.kuozhan; import java.io.File; import java.util.Scanner; /*键盘录 ...
- sqlserver同一个局域网内,把服务器数据库备份到客户端
1.客户端主机创建网络共享文件夹 2.远程服务器运行: EXEC sp_configure 'show advanced options', 1;-- 允许配置高级选项--配置选项'show adva ...
- cmd查看同一个局域网内电脑IP
win+R,cmd #快速打开cmd窗口 net view #查看本地局域网内开启了哪些计算机共享 运行后可以看到已共享的计算机名称 net view ip #查看对方局域网内开启了哪些共享 ...
- VirtualBox内Linux系统与Windows共享文件夹
在日常工作或学习中我们经常需要在一台电脑上同时使用Windows和Linux(这里以Ubuntu为例)两个系统,我们通常的做法有两种: 一种安装双系统(双系统的安装方法经验里已经有很多,大家可以去参照 ...
随机推荐
- MySQL数据库的基本语法
1.MySQL数据类型数值以及浮点型介绍 2.MySQL数据类型之字符串介绍 常用的有:char.varchar.text. 3.MySQL数据类型之时间类型介绍 常用的是:timestampt,将时 ...
- PHP算法之IP 地址无效化
给你一个有效的 IPv4 地址 address,返回这个 IP 地址的无效化版本. 所谓无效化 IP 地址,其实就是用 "[.]" 代替了每个 ".". 示例 ...
- virtualbox启动虚机报错:The VM session was closed before any attempt to power it on.
解决方法: image.png 点击清除即可. 或者在控制>清除保存的状态.然后重启虚机即可!
- SQL Server - SQL Server/ bcp 工具如何通信
问题-BCP通讯 ref: https://stackoverflow.com/questions/40664708/bcp-cannot-connect-to-aws-sql-server-but- ...
- QT安装以及使用(QT支持linux和windows,也支持C/C++代码的编译运行,比vs简洁多)
Windows: 0. QT Versionqt-win-opensource-4.7.4-mingwqt-creator-win-opensource-2.4.1 1. 系统Windows 7 &a ...
- 单调栈——cf777E
傻逼单调栈啊我怎么想了半天dp #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef st ...
- SPSS分类分析:决策树
SPSS分类分析:决策树 一.决策树(分析-分类-决策树) "决策树"过程创建基于树的分类模型.它将个案分为若干组,或根据自变量(预测变量)的值预测因变量(目标变量)的值.此过程为 ...
- day23_2_logging
#!/usr/bin/env python# -*- coding:utf-8 -*-# ------------------------------------------------------- ...
- Python学习之文件操作(二)
CSV文件处理 在Python中处理CSV文件可以使用模块csv.有关csv模块的官方资料看这里. 1 读取csv文件 csv.reader(csvfile, dialect='excel', **f ...
- jquery 表单验证插件
其他: <form action=""> First name: <input type="text" name="FirstNam ...