IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> SMB操作远程文件 -> 正文阅读

[游戏开发]SMB操作远程文件

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;


/**
 * 根据文件路径找到共享文件,并修改参数值
 *
 * @author Chen
 * @date 2022/4/1
 */
@Slf4j
public class UpdateShareFileDataUtil {

    private static String TARGET_URL = "smb://***:***@***/share/java";

    /**
     * 获取远程文件,并根据参数值更新文本内容上传到指定路径
     *
     * @param filePath 本地格式存储的文件路径 
     * @param paramMap 需要更改的参数Map<参数名称,新参数值>
     */
    @SuppressWarnings("unused")
    public static void smbFile(String filePath, HashMap<String, String> paramMap) {

        //初始化远程目录信息,将filePath格式转换成正确的  replace所有 \ 变成 /,去掉前两个字符
        //根据自己项目需要进行转换
        String remotePath = filePath.replace("\\", "/").substring(2);

        //按smb格式拼接相关信息  smb://user:password@ip/testIndex/test.txt
        String remoteUrl = "smb://user:password@" + remotePath;
        String localDir = "E:\\tmp";

        //先下载到本地
        smbGet(remoteUrl, localDir);
        //更改内容,把filePath 文件名称前面无用的去除
        String fileName = localDir + "\\" + filePath.substring(filePath.lastIndexOf("\\") + 1);
        updateFile(fileName, paramMap);
    }

    /**
     * 从共享目录下载文件到本地
     *
     * @param remoteUrl
     * @param localDir
     */
    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();
            }
        }


    }

    /**
     * 修改本地文件内容,根据要求从values中匹配对应的参数进行重新赋值
     * 这里是按每一行开头是否符合正则表达式进行修改,具体按个人需要
     *
     * @param localUrl
     * @param paramMap 
     */
    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()){
                    //判断参数map中是否有对应的key,有就更新参数值,截取 = 号前的值
                    String param = lineValue.substring(0, lineValue.lastIndexOf("="));
                    if (paramMap.containsKey(param)) {
                        //如果有,则将param.value更换到 = 号后面 ; 前面的值
                        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);
    }


    /**
     * 将本地文件上传至共享目录
     *
     * @param remoteUrl
     * @param localFilePath
     */
    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);
    }

}

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-04-04 12:42:53  更:2022-04-04 12:44:32 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年10日历 -2024/10/27 18:26:57-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码