SMB远程操作文件
- 下载远程文件,逐行修改符合条件的行内容,将修改完的文件重新上传到指定远程目录下
主要注意访问的url格式为:
smb://账号user:密码password@访问的ip/要访问的文件路径/文件.txt
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Slf4j
public class UpdateShareFileDataUtil {
private static String TARGET_URL = "smb://***:***@***/share/java";
@SuppressWarnings("unused")
public static void smbFile(String filePath, HashMap<String, String> paramMap) {
String remotePath = filePath.replace("\\", "/").substring(2);
String remoteUrl = "smb://user:password@" + remotePath;
String localDir = "E:\\tmp";
smbGet(remoteUrl, localDir);
String fileName = localDir + "\\" + filePath.substring(filePath.lastIndexOf("\\") + 1);
updateFile(fileName, paramMap);
}
private static void smbGet(String remoteUrl, String localDir) {
InputStream in = null;
OutputStream out = null;
try {
SmbFile remoteFile = new SmbFile(remoteUrl);
if (remoteFile == null) {
log.info("共享文件不存在");
return;
}
String fileName = remoteFile.getName();
File localFile = new File(localDir + File.separator + fileName);
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];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void updateFile(String localUrl, HashMap<String, String> paramMap) {
try {
String regex = "^#[0-9]=";
Pattern pattern = Pattern.compile(regex);
File file = new File(localUrl);
List<String> list = FileUtils.readLines(file,"UTF-8");
for (int i = 0; i < list.size(); i++){
String lineValue = list.get(i);
Matcher marcher = pattern.matcher(lineValue);
if (marcher.find()){
String param = lineValue.substring(0, lineValue.lastIndexOf("="));
if (paramMap.containsKey(param)) {
String newValue = param + "=" + paramMap.get(param) + ";";
list.remove(i);
list.add(i,newValue);
}
}
}
FileUtils.writeLines(file, "UTF-8", list, false);
} catch (IOException e) {
log.info("更改参数值失败");
}
smbPut(TARGET_URL, localUrl);
}
private static void smbPut(String remoteUrl, String localFilePath) {
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);
String fileName = localFile.getName();
SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName);
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];
}
} catch (Exception e) {
log.info("文件上传失败");
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
HashMap<String,String> map = new HashMap<>(8);
map.put("#1","22222.");
map.put("#3","44444");
String filePath = "\\\\ip\\【Java从入门到入坟】.nc";
smbFile(filePath,map);
}
}
|