1:安裝NFS
(1)安裝
yum install nfs-utils rpcbind
(2)啟動rpcbind服務
systemctl restart rpcbind.service
查看服務狀態
systemctl status rpcbind.service
查看rpc
lsof -i :111
netstat -lntup|grep rpcbind
(3)啟動NFS服務
systemctl start nfs.service
查看狀態
systemctl status nfs.service
查看rpc注冊的端口信息
rpcinfo -p localhost
(4)啟動順序一定是rpcbind->nfs,否則有可能出現錯誤
systemctl start rpcbind.service
systemctl enable rpcbind.service
systemctl start nfs.service
systemctl enable nfs.service
(5)配置端口
nfs除了主程序端口2049和rpcbind的端口111是固定以外,還會使用一些隨機端口,以下配置將定義這些端口,以便配置防火牆。
MOUNTD_PORT=4001
STATD_PORT=4002
LOCKD_TCPPORT=4003
LOCKD_UDPPORT=4003
RQUOTAD_PORT=4004
(6)配置
/home/wzh/nfs 192.168.0.0/24(rw,sync,insecure,no_root_squash)
/home/wzh/nfs 192.168.3.0/24(rw,sync,insecure,no_root_squash)
exportfs -r #重載exports配置
exportfs -v #查看共享參數
2:Windows10系統下面掛載測試
C:\Users\yan>mount \\192.168.0.XXX\home\wzh\nfs\ x:
x: 現已成功連接到 \\192.168.0.XXX\home\wzh\nfs\
命令已成功完成。
C:\Users\yan>
3:解決客戶端無法寫入的問題
[wzh@centos-oracle ~]$ chmod 777 nfs/
4:通過Java進行文件上傳下載
(1)工具包
commons-lang-2.6.jar
netty-3.2.8.Final.jar
nfs-client-1.0.3.jar
slf4j-api-1.7.25.jar
(2)NfsUtil.java
package com.test; import com.emc.ecs.nfsclient.nfs.NfsCreateMode; import com.emc.ecs.nfsclient.nfs.NfsSetAttributes; 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.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; /** * @作者 y * @版本 V1.0 * @描述 NFS工具類 */ public class NfsUtil { private static final String NFS_IP = "192.168.0.XXX"; private static final String NFS_DIR = "/home/wzh/nfs"; /** * 上傳文件到NFS服務器 * @param path NFS 存儲的相對路徑 * @param fileName 文件名稱包括文件后綴 * @param content 文件二進制內容 * @return */ public static boolean upload(String path, String fileName, byte []content){ NfsFileOutputStream outputStream = null; NfsSetAttributes nfsSetAttr = new NfsSetAttributes(); nfsSetAttr.setMode((long) (0x00100 + 0x00080 + 0x00040 + 0x00020 + 0x00010 + 0x00008 + 0x00004 + 0x00002)); try { Nfs3 nfs3 = new Nfs3(NFS_IP, NFS_DIR, new CredentialUnix(-2, -2, null), 3); String paths[] = path.substring(1).split("/");//去掉第一個/之后進行分割處理 StringBuilder p = new StringBuilder(); //首先判斷目錄是否存在,如果不存在則進行創建目錄 for(String s:paths){ p.append("/").append(s); Nfs3File filePath = new Nfs3File(nfs3, p.toString()); if (!filePath.exists()) { filePath.mkdir(nfsSetAttr); } } //創建文件 Nfs3File desFile = new Nfs3File(nfs3, path+"/"+fileName); desFile.create(NfsCreateMode.GUARDED, nfsSetAttr, null); outputStream = new NfsFileOutputStream(desFile); outputStream.write(content); return true; } catch (IOException ex) { Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex); } finally{ if(null!=outputStream){ try { outputStream.close(); } catch (IOException ex) { Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex); } } } return false; } /** * 文件下載 * @param filePath NFS上面的文件路徑信息 * @return */ public static byte[] download(String filePath){ ByteArrayOutputStream bos = null; NfsFileInputStream inputStream = null; BufferedInputStream bis = null; try { Nfs3 nfs3 = new Nfs3(NFS_IP, NFS_DIR, new CredentialUnix(-2, -2, null), 3); Nfs3File file = new Nfs3File(nfs3, filePath); inputStream = new NfsFileInputStream(file); bis = new BufferedInputStream(inputStream); bos = new ByteArrayOutputStream(); int date = -1; while ((date = bis.read()) != -1) { bos.write(date); } return bos.toByteArray(); } catch (IOException ex) { Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex); } finally{ if(null!=bos){ try { bos.close(); } catch (IOException ex) { Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex); } } if(null!=bis){ try { bis.close(); } catch (IOException ex) { Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex); } } if(null!=inputStream){ try { inputStream.close(); } catch (IOException ex) { Logger.getLogger(NfsUtil.class.getName()).log(Level.SEVERE, null, ex); } } } return null; } }
(3)測試類FileTest.java
package com.test; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.logging.Level; import java.util.logging.Logger; /** * @作者 y * @版本 V1.0 * @描述 */ public class FileTest { public static void main(String[] args) { String fileName = "wszm.pdf"; int hashcode = fileName.hashCode(); int dir1 = hashcode & 0xf; //0--15 int dir2 = (hashcode & 0xf0) >> 4; //0-15 String path = "/" + dir1 + "/" + dir2; byte []file = fileToBytes("G:\\tmp\\wszm.pdf"); boolean flag = NfsUtil.upload(path, fileName, file); System.out.println("flag:"+flag); for(int i=0;i<3;i++){ byte []buff = NfsUtil.download("/t01/t001/tt/ssptbin20180613.7z"); bytesToFile(buff,"G:\\tmp\\ssptbin20180613"+i+".7z"); } } public static void bytesToFile(byte[] buffer, final String filePath){ File file = new File(filePath); OutputStream output = null; BufferedOutputStream bufferedOutput = null; try { output = new FileOutputStream(file); bufferedOutput = new BufferedOutputStream(output); bufferedOutput.write(buffer); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ if(null!=bufferedOutput){ try { bufferedOutput.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != output){ try { output.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static byte[] fileToBytes(String filePath) { byte[] buffer = null; File file = new File(filePath); FileInputStream fis = null; ByteArrayOutputStream bos = null; try { fis = new FileInputStream(file); bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } buffer = bos.toByteArray(); } catch (FileNotFoundException ex) { Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex); } finally { try { if (null != bos) { bos.close(); } } catch (IOException ex) { Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex); } finally{ try { if(null!=fis){ fis.close(); } } catch (IOException ex) { Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex); } } } return buffer; } }
