java訪問windows遠端共享檔案的方法
阿新 • • 發佈:2018-12-11
沒密碼的,直接用url訪問就可以了,跟本地檔案相同。
有密碼的,用smb。
遠端需要開啟smb。
win10啟動smb的方法:
https://jingyan.baidu.com/article/47a29f2477fb56c014239996.html
注意,配完必須要重啟windows。
smb需要在伺服器端啟動,也就是那臺windows機器,不啟動會報錯 Software caused connection abort: recv failed
package file; import jcifs.smb.*; import java.io.*; public class ShareFile { /** * 一個普通的讀取遠端共享檔案的方法,不需要輸入密碼 * @param sourceFilePath * @param encode * @throws IOException */ public static void readFile(String sourceFilePath, String encode) throws IOException { File file = new File(sourceFilePath); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encode)); StringBuilder strBuilder = new StringBuilder(); String sLine = null; while ((sLine = br.readLine()) != null) { strBuilder.append(sLine); strBuilder.append("\r\n"); } br.close(); System.out.println(strBuilder.toString()); } /** * 一個普通的讀取遠端共享檔案並儲存到本地的方法,不需要輸入密碼 * @param srcFileUrl * @param dstFilePath * @param dstFileName * @throws IOException */ public static void readAddSaveFile(String srcFileUrl, String dstFilePath,String dstFileName) throws IOException { File dstPath = new File(dstFilePath); if (!dstPath.exists()) { dstPath.mkdirs(); } OutputStream outputStream = new FileOutputStream(dstPath.getPath() + File.separator + dstFileName); // 開始讀取 int len; byte[] bs = new byte[1024]; // 1K的資料緩衝 File srcFile = new File(srcFileUrl); InputStream inputStream = new FileInputStream(srcFile); while ((len = inputStream.read(bs)) != -1) { outputStream.write(bs, 0, len); } inputStream.close(); outputStream.flush(); outputStream.close(); } /** * 一個往遠端smb檔案寫資料的方法。 * @throws IOException */ public static void getFileBySmb() throws IOException { String user = "Felicia_shi"; String pass = "123456"; String sharedFolder = "Share"; String path = "smb://1.1.1.109/" + sharedFolder + "/test.txt"; NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", user, pass); SmbFile smbFile = new SmbFile(path, auth); System.out.println(smbFile.getDfsPath()); ; SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile); smbfos.write("testing.and writing to a file".getBytes()); System.out.println("completed nice !"); } /** * 遠端檔案路徑。賬號密碼可以用這個url裡面配置的,也可以用上面的NtlmPasswordAuthentication方式 */ private static String smbUrl = "smb://Felicia_shi:[email protected]/Share"; /** * 在遠端smb路徑下建立路徑 * @param dir * @throws Exception */ public static void createDir(String dir) throws Exception{ SmbFile fp = new SmbFile(smbUrl +"/"+dir); System.out.println("fieldir+++++++++++++++++++++="+ smbUrl +"/"+dir); //File fp = new File("Z://"+dir); // 目錄已存在建立資料夾 if (fp.exists() && fp.isDirectory()) { } else{ // 目錄不存在的情況下,會丟擲異常 fp.mkdir(); } } /** * 拷貝smb下一個檔案到本地。dstFileName為本地帶檔名的路徑,smbUrl為遠端路徑,檔名一樣。 * 這麼寫比較死,可以把檔名和路徑分開放。 * @param dstFileName */ public static void copyDir(String dstFileName) { InputStream in = null; OutputStream out = null; try{ File fp = new File(dstFileName); SmbFile remoteFile = new SmbFile(smbUrl +"//"+fp.getName()); System.out.println("remoteFile+++++++++++++++++++++="+remoteFile); in = new BufferedInputStream(new SmbFileInputStream(remoteFile)); out = new BufferedOutputStream(new FileOutputStream(fp)); // in = new BufferedInputStream(new FileInputStream(fp)); // out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile)); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } // 重新整理此緩衝的輸出流 out.flush(); }catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 獲取本段路徑下的所有檔名 * @param path * @return */ public static String[] getFileNames(String path) { File dirFile = new File(path); if (dirFile.isDirectory()) { File[] files = dirFile.listFiles(); String[] fileNames = new String[files.length]; for (int i = 0; i < files.length; i++) { fileNames[i] = files[i].getAbsolutePath(); } return fileNames; } else { return null; } } public static void main(String[] args) { // //讀取單個檔案 // try { // ShareFile.readFile("\\\\1.1.1.110\\sfx\\gx\\11.txt","utf-8"); // } catch (IOException e) { // e.printStackTrace(); // } // //讀取某個目錄下所有檔案 // // String[] fileNames = getFileNames("\\\\1.1.1.110\\sfx\\gx"); // String encode = "utf-8"; // for (String fileName : fileNames) { // try { // readFile(fileName, encode); // } catch (IOException e) { // e.printStackTrace(); // } // } // try { // readAddSaveFile("\\\\1.1.1.110\\sfx\\gx\\a.pdf","\\\\1.1.1.110\\sfx\\gx1","a.pdf"); // } catch (IOException e) { // e.printStackTrace(); // } // try { // getFileBySmb(); // createDir("111"); // } catch (IOException e) { // e.printStackTrace(); // } catch (Exception e) { // e.printStackTrace(); // } copyDir("E:\\\\sfx\\gx1\\a.pdf"); } }