由于Windows系统设计的特殊性,不像我们常用的Linux系统那样,可通过默认SSH、SFTP等方式连接就可直接上传文件。所以这里选择的是使用Windows共享目录的方式,是不需要安装任何东西,使用共享目录的方式,只可以上传下载删除文件 (当然也可以通过SSH等方式连接Windows系统,进行CMD命令的操作以及文件的上传下载等,但是是需要安装软件的,比如使用:freeSSHd,具体可以看我另一篇文章)

第一步【准备工作】:查看你准备操作的Windows是否默认有共享目录。(系统默认会有)

Windows系统默认是会共享每个盘的根目录的,但是每个人的情况不同,最好还是使用 net share 命令确定一下。如果没有的话,可以先提前手动设置好共享目录,具体如何设置Windows共享目录,大家可自行百度。

Java上传文件到OSS java上传文件到nas_linux


这里显示有默认共享就OK了。

第二步【准备工作】:查看Windows的Server服务是否开启。(默认自动开启)

没记错的话,Windows是默认会开启445端口的,我们今天说的通过共享目录上传文件就是使用445端口。所以这里的Server服务就是核心,必须开启。当然Windows也是默认开启的,但是为了少走坑,最好还是看一下。。。。

Java上传文件到OSS java上传文件到nas_Java上传文件到OSS_02

第三步【代码操作】:Maven引入依赖

以上两步都确定没问题了以后,就在项目中引入依赖准备开发。

<dependency>
	  <groupId>jcifs</groupId>
	  <artifactId>jcifs</artifactId>
	  <version>1.3.17</version>
</dependency>

第四步【代码操作】:编写代码

  1. 从本地上传文件到共享目录
/**
     * Description: 从本地上传文件到共享目录
     * @param remoteUrl 共享文件目录
     * @param localFilePath 本地文件绝对路径
     */
    public void smbPut(String remoteUrl,String localFilePath, String userName, String pwd) {
        InputStream in = null;
        OutputStream out = null;
        try {
            File localFile = new File(localFilePath);

            String fileName = localFile.getName();
            //这里推荐使用这种方式进行用户名密码的校验,在url中拼接,如果需要特殊字符可能无法转换
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, userName, pwd);
            SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName, auth);
            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) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

2.从共享目录拷贝文件到本地

/**
     * Description: 从共享目录拷贝文件到本地
     * @param remoteUrl 共享目录上的文件路径
     * @param localDir 本地目录
     */
    public void smbGet(String remoteUrl,String localDir) {
        InputStream in = null;
        OutputStream out = null;
        try {
            SmbFile remoteFile = new SmbFile(remoteUrl);
            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();
            }
        }
    }

3.删除远程windows文件

/**
     * 删除文件
     * @param remoteUrl 共享文件目录
     * @param fileName 要删除的文件名
     */
    public static void deleteFile(String remoteUrl, String fileName, String userName, String pwd) {
        SmbFile SmbFile;
        try {
            // smb://userName:passWord@host/path/shareFolderPath/fileName
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, userName, pwd);
            SmbFile = new SmbFile(remoteUrl + "/" + fileName, auth);
            if (SmbFile.exists()) {
                SmbFile.delete();
            }
        } catch (MalformedURLException | SmbException e) {
            e.printStackTrace();
        }
    }
  1. 连接测试
public static void main(String[] args) throws Exception {
        WindowsUtil test = new WindowsUtil();
        //这里要注意,remoteUrl参数(也就是第一个参数),必须是有smb://前缀的,这是协议!后面拼接ip地址,再拼接的就是第一步中,共享文件夹的共享名!
        test.smbPut("smb://192.168.xx.xx/C$/",
                "D:/localhostTest/curl-7.70.0-win64-mingw.zip",
                "administrator","password");
    }

总结

到这里就结束了。
可能你们开发的过程中会遇到报错,但是必须要注意的是,连接服务器的账号和密码,最好是administrator,不然的话可能会出现权限不够的问题。

如果你觉得帮到你了,麻烦请点个赞!谢谢~