JAVA上傳文件到Windows服務器_共享目錄(詳細教程)

由於Windows系統設計的特殊性,不像我們常用的Linux系統那樣,可通過默認SSH、SFTP等方式連接就可直接上傳文件。所以這裏選擇的是使用Windows共享目錄的方式,是不需要安裝任何東西,使用共享目錄的方式,只可以上傳下載刪除文件 (當然也可以通過SSH等方式連接Windows系統,進行CMD命令的操作以及文件的上傳下載等,但是是需要安裝軟件的,比如使用:freeSSHd,具體可以看我另一篇文章)

第一步【準備工作】:查看你準備操作的Windows是否默認有共享目錄。(系統默認會有)

Windows系統默認是會共享每個盤的根目錄的,但是每個人的情況不同,最好還是使用 net share 命令確定一下。如果沒有的話,可以先提前手動設置好共享目錄,具體如何設置Windows共享目錄,大家可自行百度。
查看是否有共享目錄
這裏顯示有默認共享就OK了。

第二步【準備工作】:查看Windows的Server服務是否開啓。(默認自動開啓)

沒記錯的話,Windows是默認會開啓445端口的,我們今天說的通過共享目錄上傳文件就是使用445端口。所以這裏的Server服務就是核心,必須開啓。當然Windows也是默認開啓的,但是爲了少走坑,最好還是看一下。。。。
在這裏插入圖片描述

第三步【代碼操作】: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,不然的話可能會出現權限不夠的問題。

如果你覺得幫到你了,麻煩請點個贊!謝謝~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章