注:1.若用户是管理员(域中)可能会导致无法正常读取文件。建议[新建一个账户](https://www.jianshu.com/p/407aa3069d83)试一试
2.如果密码带有#,smb用以下方式无法正确解析,查看源码会发现#是关键字,最终解析的密码不会带上#,可以使用
NtlmPasswordAuthentication au = new NtlmPasswordAuthentication(user:pwd);
SmbFile remoteFile = new SmbFile(fromPath,au);
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
/**
* Windows共享文件操作工具类
*
*
* @author archie
* @date 2018-11-19
*/
public class SmbFileUtil {
/**
* 共享文件夹配置
*/
private static final String HOST = "";
private static final String USER_NAME = "";
private static final String PASSWORD = "";
private static final String PATH = "";
private static final String REMOTE_URL = "smb://" + USER_NAME + ":" + PASSWORD + "@" + HOST + PATH;
/**
* 下载文件
*
* @param fileName 文件名 + .文件类型 1.txt
* @param localDir 文件保存文件夹
* @throws IOException
*/
public static String smbGet(String fileName, String localDir) throws IOException {
InputStream in = null;
OutputStream out = null;
StringBuffer result = new StringBuffer();
try {
SmbFile remoteFile = new SmbFile(REMOTE_URL + fileName);
if (remoteFile == null || !remoteFile.exists()) {
//文件不存在
return "";
}
result.append(localDir).append(File.separator).append(fileName);
File localFile = new File(result.toString());
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
out = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[1024];
int bytesToRead = -1;
while ((bytesToRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
return result.toString();
}
/**
* 上传文件
*
* @param localFilePath 文件路径 + 文件名 + .文件类型
* @throws IOException
*/
public static void smbPut(String localFilePath) throws IOException {
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);
String fileName = localFile.getName();
SmbFile remoteFile = new SmbFile(REMOTE_URL + fileName);
in = new BufferedInputStream(new FileInputStream(localFile));
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
byte[] buffer = new byte[1024];
int bytesToRead = -1;
while ((bytesToRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
/**
* 远程文件删除 文件名 + .文件类型
*
* @param fileName
*/
public static void smbDelete(String fileName) {
SmbFile file = null;
try {
file = new SmbFile(REMOTE_URL + fileName);
if (file.exists()) {
file.delete();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (SmbException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
//smbGet("105TA919-4.jpg","e:");
smbPut("E:\\scooper\\pom.xml");
//smbDelete("1.txt");
}
}
网友评论