Javaはnfs-clientパッケージに基づいており、ファイルのアップロードとダウンロードの操作ソースコードのためにLinuxサーバーに接続するためのNFSプロトコルを実現します。



Java Is Based Nfs Client Package Realize Nfs Protocol Connect Linux Server



依存関係を追加する

// https://mvnrepository.com/artifact/com.emc.ecs/nfs-client compile group: 'com.emc.ecs', name: 'nfs-client', version: '1.0.3'

コード例



package com.xl import com.emc.ecs.nfsclient.nfs.io.Nfs3File import com.emc.ecs.nfsclient.nfs.io.NfsFileInputStream import com.emc.ecs.nfsclient.nfs.io.NfsFileOutputStream import com.emc.ecs.nfsclient.nfs.nfs3.Nfs3 import com.emc.ecs.nfsclient.rpc.CredentialUnix import java.io.* public class NfsTransferFile { private static final String NFS_IP = '192.168.0.101' private static final String NFS_DIR = '/data/nfs' public static void main(String[] args) { uploadFileToNfs() //downLoadFileFromNfs() } //Upload local files to the specified directory of the Nfs server public static void uploadFileToNfs() { String localDir = 'F:\look\look.txt' InputStream inputStream = null OutputStream outputStream = null try { //Create a local file object File localFile = new File(localDir) //Get the file name of the local file, this name is used to create a file with the same name in the specified directory on the remote Nfs server String localFileName = localFile.getName() Nfs3 nfs3 = new Nfs3(NFS_IP, NFS_DIR, new CredentialUnix(0, 0, null), 3) //Create Nfs file object on remote server Nfs3File NfsFile = new Nfs3File(nfs3, '/' + localFileName) //Open a file input stream inputStream = new BufferedInputStream(new FileInputStream(localFile)) //Open a remote Nfs file output stream and copy the file to the destination outputStream = new BufferedOutputStream(new NfsFileOutputStream(NfsFile)) //Buffer memory byte[] buffer = new byte[1024] while ((inputStream.read(buffer)) != -1) { outputStream.write(buffer) } System.out.println('File upload complete!') } catch (Exception ex) { ex.printStackTrace() } finally { try { if (outputStream != null) { outputStream.close() } if (inputStream != null) { inputStream.close() } } catch (IOException ex) { ex.printStackTrace() } } } //Download the specified file from the Nfs server to the local directory public static void downLoadFileFromNfs() { String NfsFileDir = '/look.txt' String localDir = 'F:\look\' InputStream inputStream = null OutputStream outputStream = null try { Nfs3 nfs3 = new Nfs3(NFS_IP, NFS_DIR, new CredentialUnix(0, 0, null), 3) //Create Nfs file object on remote server Nfs3File nfsFile = new Nfs3File(nfs3, NfsFileDir) String localFileName = localDir + nfsFile.getName() //Create a local file object File localFile = new File(localFileName) //Open a file input stream inputStream = new BufferedInputStream(new NfsFileInputStream(nfsFile)) //Open a remote Nfs file output stream and copy the file to the destination outputStream = new BufferedOutputStream(new FileOutputStream(localFile)) //Buffer memory byte[] buffer = new byte[1024] while (inputStream.read(buffer) != -1) { outputStream.write(buffer) } System.out.println('File download complete!') } catch (IOException ex) { ex.printStackTrace() } finally { try { if (outputStream != null) { outputStream.close() } if (inputStream != null) { inputStream.close() } } catch (Exception e) { e.printStackTrace() } } } }