同一个局域网内,使用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:\\⽂档\\");
String host = "192.168.1.151";//远程服务器的地址
String username = "admin";//⽤户名
String password = "admin";//密码
国家安全手抄报String path = "/share/";//远程服务器共享⽂件夹名称
String remoteUrl = "smb://" + username + ":" + password + "@" + host + path + (dsWith("/") ? "" : "/");
SmbFile remoteFile = new SmbFile(remoteUrl + "/" + Name());
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;
2020国庆放假安排几天
import java.io.*;
/**uefi
* <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 + (dsWith("/") ? "" : "/");
//创建远程⽂件对象
SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName);
//创建⽂件流
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 的pyFile(final File srcFile, final File destFile);和 java 1.7 的 py(Path source, Path target, options);但是由于SmbFile 和 File 不属于⼀个类型,导致失败,最后只能选择使⽤流的⽅式进⾏操作。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。