この例では、SMBを使用してJavaの特定のコードを共有して、参照のリモートファイルを読み取ります。特定のコンテンツは次のとおりです
パッケージcom.yss.test.filereadwriter; java.io.bufferedinputStreamをインポートします。 java.io.bufferedOutputStreamをインポートします。 java.io.fileをインポートします。 java.io.fileinputStreamをインポートします。 java.io.fileoutputStreamをインポートします。 java.io.ioexceptionをインポートします。 java.io.inputStreamをインポートします。 java.io.outputStreamをインポートします。 java.net.malformedurlexceptionをインポートします。 jcifs.smb.smbfileをインポートします。 JCIFS.SMB.SMBFILEINPUTSTREAMをインポートします。 jcifs.smb.smbfileoutputStreamをインポートします。パブリッククラスRemoteaCcessData {/** * @param args * @throws ioexception */public static void main(string [] args)throws ioexception {smbget1( "smb://192.168.75.204/test/new text document.txt"); smbget( "smb://192.168.75.204/test/new text document.txt"、 "e:/"); }/** *方法1: * * @param remoteurl *リモートパスSMB://192.168.75.204/test/New Text Document.txt * @Throws IOException */public Static void smbget1(String remoteurl)Throws ioexception {smbfile smbfile(remoturell); int length = smbfile.getContentLength(); //ファイルサイズバイトバッファー[] = new byte [length]; smbFileInputStream in = new SmbFileInputStream(SMBFILE); // smbファイル入力ストリームを作成しますwhile((in.read(buffer))!= -1){system.out.write(buffer); System.out.println(buffer.length); } in.close(); } //共有ディレクトリからファイルのダウンロードstring localdir){inputstream in = null; outputStream out = null; try {smbfile remotefile = new smbfile(remoteurl); if(remotefile == null){system.out.println( "共有ファイルは存在しません");戻る; }文字列filename = remotefile.getName();ファイル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);バッファー=新しいバイト[1024]; }} catch(例外e){e.printstacktrace(); }最後に{try {out.close(); in.close(); } catch(ioexception e){e.printstacktrace(); }}} //ファイルをアップロードpublic static void smbput(string remoteurl、string localfilepath){inputstream in = null; outputStream out = null; try {file localfile = new file(localfilepath);文字列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);バッファー=新しいバイト[1024]; }} catch(例外e){e.printstacktrace(); }最後に{try {out.close(); in.close(); } catch(ioexception e){e.printstacktrace(); }}} //リモートURL smb://192.168.0.77/test //ユーザー名とパスワードが必要な場合、このように:// smb:// username:[email protected]/test}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。